Skip to content

Commit a4e6cdd

Browse files
committed
First version of sendPhoto by URL
1 parent 784a784 commit a4e6cdd

File tree

3 files changed

+146
-1
lines changed

3 files changed

+146
-1
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*******************************************************************
2+
* An example of how to use a custom reply keyboard markup. *
3+
* *
4+
* *
5+
* written by Vadim Sinitski *
6+
*******************************************************************/
7+
#include <ESP8266WiFi.h>
8+
#include <WiFiClientSecure.h>
9+
#include <UniversalTelegramBot.h>
10+
11+
// Initialize Wifi connection to the router
12+
char ssid[] = "XXXXXX"; // your network SSID (name)
13+
char password[] = "YYYYYY"; // your network key
14+
15+
// Initialize Telegram BOT
16+
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
17+
18+
WiFiClientSecure client;
19+
UniversalTelegramBot bot(BOTtoken, client);
20+
21+
int Bot_mtbs = 1000; //mean time between scan messages
22+
long Bot_lasttime; //last time messages' scan has been done
23+
24+
String test_image_url = "https://www.arduino.cc/en/uploads/Trademark/ArduinoCommunityLogo.png";
25+
26+
void handleNewMessages(int numNewMessages) {
27+
Serial.println("handleNewMessages");
28+
Serial.println(String(numNewMessages));
29+
30+
for (int i=0; i<numNewMessages; i++) {
31+
String chat_id = String(bot.messages[i].chat_id);
32+
String text = bot.messages[i].text;
33+
34+
String from_name = bot.messages[i].from_name;
35+
if (from_name == "") from_name = "Guest";
36+
37+
if (text == "/get_test_image") {
38+
bot.sendImageAsURL(chat_id, test_image_url, "Caption is optional, you may not use image caption");
39+
}
40+
41+
if (text == "/start") {
42+
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
43+
welcome += "This is Send Image From URL example.\n\n";
44+
welcome += "/get_test_image : getting test image\n";
45+
//welcome += "You will receive image from " + test_image_url + "\n";
46+
47+
bot.sendMessage(chat_id, welcome, "");
48+
}
49+
}
50+
}
51+
52+
void setup() {
53+
Serial.begin(115200);
54+
55+
// Set WiFi to station mode and disconnect from an AP if it was Previously connected
56+
WiFi.mode(WIFI_STA);
57+
WiFi.disconnect();
58+
delay(100);
59+
60+
// attempt to connect to Wifi network:
61+
Serial.print("Connecting Wifi: ");
62+
Serial.println(ssid);
63+
WiFi.begin(ssid, password);
64+
65+
while (WiFi.status() != WL_CONNECTED) {
66+
Serial.print(".");
67+
delay(500);
68+
}
69+
70+
Serial.println("");
71+
Serial.println("WiFi connected");
72+
Serial.print("IP address: ");
73+
Serial.println(WiFi.localIP());
74+
}
75+
76+
void loop() {
77+
if (millis() > Bot_lasttime + Bot_mtbs) {
78+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
79+
80+
while(numNewMessages) {
81+
Serial.println("got response");
82+
handleNewMessages(numNewMessages);
83+
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
84+
}
85+
86+
Bot_lasttime = millis();
87+
}
88+
}

src/UniversalTelegramBot.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,27 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject& payload) {
431431
return sent;
432432
}
433433

434+
bool UniversalTelegramBot::sendPostPhoto(JsonObject& payload) {
435+
436+
bool sent=false;
437+
if (_debug) Serial.println("SEND Post Photo");
438+
long sttime=millis();
439+
440+
if (payload.containsKey("photo")) {
441+
while (millis() < sttime+8000) { // loop for a while to send the message
442+
String command = "bot"+_token+"/sendPhoto";
443+
String response = sendPostToTelegram(command, payload);
444+
if (_debug) Serial.println(response);
445+
sent = checkForOkResponse(response);
446+
if (sent) {
447+
break;
448+
}
449+
}
450+
}
451+
452+
return sent;
453+
}
454+
434455
bool UniversalTelegramBot::sendImage(String chat_id, String contentType, int fileSize,
435456
MoreDataAvailable moreDataAvailableCallback,
436457
GetNextByte getNextByteCallback) {
@@ -446,6 +467,36 @@ bool UniversalTelegramBot::sendImage(String chat_id, String contentType, int fil
446467
return checkForOkResponse(response);
447468
}
448469

470+
bool UniversalTelegramBot::sendImageAsURL(String chat_id, String URL, String caption, bool disable_notification, int reply_to_message_id, String keyboard) {
471+
472+
DynamicJsonBuffer jsonBuffer;
473+
JsonObject& payload = jsonBuffer.createObject();
474+
475+
payload["chat_id"] = chat_id;
476+
payload["photo"] = URL;
477+
478+
if (caption) {
479+
payload["caption"] = caption;
480+
}
481+
482+
if (disable_notification) {
483+
payload["disable_notification"] = disable_notification;
484+
}
485+
486+
if (reply_to_message_id && reply_to_message_id != 0) {
487+
payload["reply_to_message_id"] = reply_to_message_id;
488+
}
489+
490+
if (keyboard) {
491+
JsonObject& replyMarkup = payload.createNestedObject("reply_markup");
492+
493+
DynamicJsonBuffer keyboardBuffer;
494+
replyMarkup["keyboard"] = keyboardBuffer.parseArray(keyboard);
495+
}
496+
497+
return sendPostPhoto(payload);
498+
}
499+
449500
bool UniversalTelegramBot::checkForOkResponse(String response) {
450501
int responseLength = response.length();
451502

src/UniversalTelegramBot.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,23 @@ class UniversalTelegramBot
5858
GetNextByte getNextByteCallback);
5959

6060
bool getMe();
61+
6162
bool sendSimpleMessage(String chat_id, String text, String parse_mode);
6263
bool sendMessage(String chat_id, String text, String parse_mode);
6364
bool sendMessageWithReplyKeyboard(String chat_id, String text,
6465
String parse_mode, String keyboard, bool resize = false,
6566
bool oneTime = false, bool selective = false);
6667
bool sendMessageWithInlineKeyboard(String chat_id, String text,
6768
String parse_mode, String keyboard);
69+
6870
bool sendPostMessage(JsonObject& payload);
71+
bool sendPostPhoto(JsonObject& payload);
6972
bool sendImage(String chat_id, String contentType, int fileSize,
7073
MoreDataAvailable moreDataAvailableCallback,
7174
GetNextByte getNextByteCallback);
75+
bool sendImageAsURL(String chat_id, String URL, String caption,
76+
bool disable_notification = false, int reply_to_message_id = 0, String keyboard = "");
77+
7278
int getUpdates(long offset);
7379
telegramMessage messages[HANDLE_MESSAGES];
7480
long last_message_received;
@@ -81,7 +87,7 @@ class UniversalTelegramBot
8187
Client *client;
8288
const int maxMessageLength = 1000;
8389
bool checkForOkResponse(String response);
84-
bool _debug = false;
90+
bool _debug = true;
8591
};
8692

8793
#endif

0 commit comments

Comments
 (0)