Skip to content

Commit 5267736

Browse files
authored
Merge pull request witnessmenow#27 from witnessmenow/feature_sendImage
Changes for sendPhoto
2 parents 52b19a6 + ede529e commit 5267736

File tree

8 files changed

+562
-27
lines changed

8 files changed

+562
-27
lines changed

.travis.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ cache:
99
- "~/.platformio"
1010

1111
env:
12-
- SCRIPT=platformioSingle EXAMPLE_FOLDER=EchoBot EXAMPLE_FILE=EchoBot BOARDTYPE=ESP8266 BOARD=d1_mini
13-
- SCRIPT=platformioSingle EXAMPLE_FOLDER=CustomKeyboard/ReplyKeyboardMarkup EXAMPLE_FILE=ReplyKeyboardMarkup BOARDTYPE=ESP8266 BOARD=d1_mini
14-
- SCRIPT=platformioSingle EXAMPLE_FOLDER=CustomKeyboard/InlineKeyboardMarkup EXAMPLE_FILE=InlineKeyboardMarkup BOARDTYPE=ESP8266 BOARD=d1_mini
15-
- SCRIPT=platformioSingle EXAMPLE_FOLDER=FlashledBot EXAMPLE_FILE=FlashledBot BOARDTYPE=ESP8266 BOARD=d1_mini
16-
- SCRIPT=platformioSingle EXAMPLE_FOLDER=UsingWiFiManager EXAMPLE_FILE=UsingWiFiManager BOARDTYPE=ESP8266 BOARD=d1_mini
17-
- SCRIPT=platformioSingle EXAMPLE_FOLDER=BulkMessages EXAMPLE_FILE=BulkMessages BOARDTYPE=ESP8266 BOARD=d1_mini
12+
- SCRIPT=platformioSingle EXAMPLE_NAME=EchoBot EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
13+
- SCRIPT=platformioSingle EXAMPLE_NAME=ReplyKeyboardMarkup EXAMPLE_FOLDER=/CustomKeyboard/ BOARDTYPE=ESP8266 BOARD=d1_mini
14+
- SCRIPT=platformioSingle EXAMPLE_NAME=InlineKeyboardMarkup EXAMPLE_FOLDER=/CustomKeyboard/ BOARDTYPE=ESP8266 BOARD=d1_mini
15+
- SCRIPT=platformioSingle EXAMPLE_NAME=FlashledBot EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
16+
- SCRIPT=platformioSingle EXAMPLE_NAME=UsingWiFiManager EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
17+
- SCRIPT=platformioSingle EXAMPLE_NAME=BulkMessages EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
18+
- SCRIPT=platformioSingle EXAMPLE_NAME=ImageFromSD EXAMPLE_FOLDER=/SendPhoto/ EXAMPLE_NAME=ImageFromSD BOARDTYPE=ESP8266 BOARD=d1_mini
19+
- SCRIPT=platformioSingle EXAMPLE_NAME=ImageFromURL EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
20+
- SCRIPT=platformioSingle EXAMPLE_NAME=ImageFromURL EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
1821

19-
# This will run all the ESP8266 examples
20-
#- SCRIPT=platformioEsp8266 BOARD=d1_mini
2122

2223
install:
2324
- pip install -U platformio
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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_photo_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_photo") {
38+
String file_id = bot.sendPhoto(chat_id, test_photo_url, "This photo was sent using URL");
39+
bot.sendPhoto(chat_id, file_id, "This photo was sent using File ID");
40+
}
41+
42+
if (text == "/start") {
43+
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
44+
welcome += "This is Send Photo From File Id example.\n\n";
45+
welcome += "/get_test_photo : getting test photo\n";
46+
//welcome += "You will receive photo from " + test_photo_url + "\n";
47+
48+
bot.sendMessage(chat_id, welcome, "");
49+
}
50+
}
51+
}
52+
53+
void setup() {
54+
Serial.begin(115200);
55+
56+
// Set WiFi to station mode and disconnect from an AP if it was Previously connected
57+
WiFi.mode(WIFI_STA);
58+
WiFi.disconnect();
59+
delay(100);
60+
61+
// attempt to connect to Wifi network:
62+
Serial.print("Connecting Wifi: ");
63+
Serial.println(ssid);
64+
WiFi.begin(ssid, password);
65+
66+
while (WiFi.status() != WL_CONNECTED) {
67+
Serial.print(".");
68+
delay(500);
69+
}
70+
71+
Serial.println("");
72+
Serial.println("WiFi connected");
73+
Serial.print("IP address: ");
74+
Serial.println(WiFi.localIP());
75+
}
76+
77+
void loop() {
78+
if (millis() > Bot_lasttime + Bot_mtbs) {
79+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
80+
81+
while(numNewMessages) {
82+
Serial.println("got response");
83+
handleNewMessages(numNewMessages);
84+
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
85+
}
86+
87+
Bot_lasttime = millis();
88+
}
89+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*******************************************************************
2+
* An example of bot that echos back any messages received *
3+
* *
4+
* written by Giacarlo Bacchio (Gianbacchio on Github) *
5+
* adapted by Brian Lough *
6+
*******************************************************************/
7+
8+
#include <ESP8266WiFi.h>
9+
#include <WiFiClientSecure.h>
10+
#include <UniversalTelegramBot.h>
11+
#include <ArduinoJson.h>
12+
13+
#include <SPI.h>
14+
#include <SD.h>
15+
16+
bool isMoreDataAvailable();
17+
byte getNextByte();
18+
19+
// Initialize Wifi connection to the router
20+
char ssid[] = "xxxxxxxxxxxxxxxxxxxxxx"; // your network SSID (name)
21+
char password[] = "yyyyyyyy"; // your network key
22+
23+
File myFile;
24+
25+
// Initialize Telegram BOT
26+
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
27+
28+
WiFiClientSecure client;
29+
UniversalTelegramBot bot(BOTtoken, client);
30+
31+
int Bot_mtbs = 1000; //mean time between scan messages
32+
long Bot_lasttime; //last time messages' scan has been done
33+
34+
35+
void setup() {
36+
37+
Serial.begin(115200);
38+
39+
Serial.print("Initializing SD card...");
40+
41+
if (!SD.begin(D0)) {
42+
Serial.println("initialization failed!");
43+
return;
44+
}
45+
Serial.println("initialization done.");
46+
47+
48+
// Set WiFi to station mode and disconnect from an AP if it was Previously
49+
// connected
50+
WiFi.mode(WIFI_STA);
51+
WiFi.disconnect();
52+
delay(100);
53+
54+
// Attempt to connect to Wifi network:
55+
Serial.print("Connecting Wifi: ");
56+
Serial.println(ssid);
57+
WiFi.begin(ssid, password);
58+
while (WiFi.status() != WL_CONNECTED) {
59+
Serial.print(".");
60+
delay(500);
61+
}
62+
Serial.println("");
63+
Serial.println("WiFi connected");
64+
Serial.println("IP address: ");
65+
IPAddress ip = WiFi.localIP();
66+
Serial.println(ip);
67+
68+
69+
}
70+
71+
bool isMoreDataAvailable(){
72+
return myFile.available();
73+
}
74+
75+
byte getNextByte(){
76+
return myFile.read();
77+
}
78+
79+
void loop() {
80+
81+
if (millis() > Bot_lasttime + Bot_mtbs) {
82+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
83+
while(numNewMessages) {
84+
Serial.println("got response");
85+
86+
87+
String chat_id = bot.messages[0].chat_id;
88+
89+
myFile = SD.open("box.jpg");
90+
if (myFile) {
91+
Serial.println("box.jpg:");
92+
93+
//Content type for PNG image/png
94+
bool sent = bot.sendPhotoByBinary(chat_id, "image/jpeg", myFile.size(),
95+
isMoreDataAvailable,
96+
getNextByte);
97+
98+
if(sent){
99+
Serial.println("Succesfully sent photo");
100+
} else {
101+
Serial.println("Error sending photo");
102+
}
103+
104+
myFile.close();
105+
} else {
106+
// if the file didn't open, print an error:
107+
Serial.println("error opening photo.jpg");
108+
}
109+
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
110+
}
111+
Bot_lasttime = millis();
112+
}
113+
}
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_photo_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_photo") {
38+
bot.sendPhoto(chat_id, test_photo_url, "Caption is optional, you may not use photo 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_photo : getting test photo\n";
45+
//welcome += "You will receive photo from " + test_photo_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+
}

scripts/travis/platformioEsp8266.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

scripts/travis/platformioSingle.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/sh -eux
22

3-
platformio ci $PWD/examples/$BOARDTYPE/$EXAMPLE_FOLDER/$EXAMPLE_FILE.ino -l '.' -b $BOARD
3+
platformio ci $PWD/examples/$BOARDTYPE$EXAMPLE_FOLDER$EXAMPLE_NAME/$EXAMPLE_NAME.ino -l '.' -b $BOARD

0 commit comments

Comments
 (0)