Skip to content

Commit fea4d09

Browse files
authored
Merge pull request witnessmenow#25 from rusboard/feature_sendImage
SendPhoto by URL, init version
2 parents 2d413fb + 35bdf02 commit fea4d09

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ env:
1616
- SCRIPT=platformioSingle EXAMPLE_FOLDER=UsingWiFiManager EXAMPLE_FILE=UsingWiFiManager BOARDTYPE=ESP8266 BOARD=d1_mini
1717
- SCRIPT=platformioSingle EXAMPLE_FOLDER=BulkMessages EXAMPLE_FILE=BulkMessages BOARDTYPE=ESP8266 BOARD=d1_mini
1818
- SCRIPT=platformioSingle EXAMPLE_FOLDER=SendPhoto/ImageFromSD EXAMPLE_FILE=ImageFromSD BOARDTYPE=ESP8266 BOARD=d1_mini
19+
- SCRIPT=platformioSingle EXAMPLE_FOLDER=SendPhoto/ImageFromURL EXAMPLE_FILE=ImageFromURL BOARDTYPE=ESP8266 BOARD=d1_mini
1920

2021
# This will run all the ESP8266 examples
2122
#- SCRIPT=platformioEsp8266 BOARD=d1_mini
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2727
#include <ArduinoJson.h>
2828
#include <Client.h>
2929

30-
#include <SD.h>
31-
3230
#define HOST "api.telegram.org"
3331
#define SSL_PORT 443
3432
#define HANDLE_MESSAGES 1
@@ -58,17 +56,23 @@ class UniversalTelegramBot
5856
GetNextByte getNextByteCallback);
5957

6058
bool getMe();
59+
6160
bool sendSimpleMessage(String chat_id, String text, String parse_mode);
6261
bool sendMessage(String chat_id, String text, String parse_mode);
6362
bool sendMessageWithReplyKeyboard(String chat_id, String text,
6463
String parse_mode, String keyboard, bool resize = false,
6564
bool oneTime = false, bool selective = false);
6665
bool sendMessageWithInlineKeyboard(String chat_id, String text,
6766
String parse_mode, String keyboard);
67+
6868
bool sendPostMessage(JsonObject& payload);
69+
bool sendPostPhoto(JsonObject& payload);
6970
bool sendImage(String chat_id, String contentType, int fileSize,
7071
MoreDataAvailable moreDataAvailableCallback,
7172
GetNextByte getNextByteCallback);
73+
bool sendImageAsURL(String chat_id, String URL, String caption,
74+
bool disable_notification = false, int reply_to_message_id = 0, String keyboard = "");
75+
7276
int getUpdates(long offset);
7377
telegramMessage messages[HANDLE_MESSAGES];
7478
long last_message_received;

0 commit comments

Comments
 (0)