Skip to content

Commit bcaca72

Browse files
committed
Splitting classes into different files
1 parent 404679b commit bcaca72

File tree

4 files changed

+213
-154
lines changed

4 files changed

+213
-154
lines changed

src/ESP8266TelegramBOT.cpp

Lines changed: 2 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -24,128 +24,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2424

2525
#include "ESP8266TelegramBOT.h"
2626

27-
const int maxBufferSize = 1000;
28-
29-
30-
TelegramBOT::TelegramBOT(String token) {
31-
_token=token;
32-
}
33-
34-
void TelegramBOT::begin(void) {
35-
36-
}
37-
38-
bool TelegramBOT::getMe() {
39-
String command="bot"+_token+"/getMe";
40-
String response = sendGetToTelegram(command); //recieve reply from telegram.org
41-
StaticJsonBuffer<500> jsonBuffer;
42-
JsonObject& root = jsonBuffer.parseObject(response);
43-
if(root.success()) {
44-
if (root.containsKey("result")) {
45-
String _name = root["result"]["first_name"];
46-
String _username = root["result"]["username"];
47-
name = _name;
48-
userName = _username;
49-
return true;
50-
}
51-
}
52-
53-
return false;
54-
}
55-
56-
/***************************************************************
57-
* GetUpdates - function to receive messages from telegram *
58-
* (Argument to pass: the last+1 message to read) *
59-
* Returns the number of new messages *
60-
***************************************************************/
61-
int TelegramBOT::getUpdates(int offset) {
62-
63-
//Serial.println("GET Update Messages ");
64-
String command="bot"+_token+"/getUpdates?offset="+String(offset)+"&limit="+String(HANDLE_MESSAGES);
65-
String response = sendGetToTelegram(command); //recieve reply from telegram.org
66-
if (response != "") {
67-
// Serial.print("incoming message length");
68-
// Serial.println(response.length());
69-
StaticJsonBuffer<maxBufferSize> jsonBuffer;
70-
71-
// Parse response into Json object
72-
JsonObject& root = jsonBuffer.parseObject(response);
73-
if(root.success()) {
74-
// root.printTo(Serial);
75-
// Serial.println();
76-
if (root.containsKey("result")) {
77-
int resultArrayLength = root["result"].size();
78-
if(resultArrayLength > 0) {
79-
int newMessageIndex = 0;
80-
for(int i=0; i < resultArrayLength; i++){
81-
int update_id = root["result"][i]["update_id"];
82-
if(last_message_recived != update_id) {
83-
last_message_recived = update_id;
84-
String text = root["result"][i]["message"]["text"];
85-
String date = root["result"][i]["message"]["date"];
86-
String chat_id = root["result"][i]["message"]["chat"]["id"];
87-
88-
messages[newMessageIndex].update_id = update_id;
89-
messages[newMessageIndex].text = text;
90-
messages[newMessageIndex].date = date;
91-
messages[newMessageIndex].chat_id = chat_id;
92-
93-
newMessageIndex++;
94-
}
95-
}
96-
return newMessageIndex;
97-
} else {
98-
Serial.println("no new messages");
99-
}
100-
} else {
101-
Serial.println("Response contained no 'result'");
102-
}
103-
} else {
104-
// Buffer may not be big enough, increase buffer or reduce max number of messages
105-
Serial.println("Failed to parse update");
106-
}
107-
108-
return 0;
109-
}
110-
}
111-
112-
/***********************************************************************
113-
* SendMessage - function to send message to telegram *
114-
* (Arguments to pass: chat_id, text to transmit and markup(optional)) *
115-
***********************************************************************/
116-
void TelegramBOT::sendMessage(String chat_id, String text, String reply_markup) {
117-
118-
bool sent=false;
119-
// Serial.println("SEND Message ");
120-
long sttime=millis();
121-
if (text!="") {
122-
while (millis()<sttime+8000) { // loop for a while to send the message
123-
String command="bot"+_token+"/sendMessage?chat_id="+chat_id+"&text="+text+"&reply_markup="+reply_markup;
124-
String mess=sendGetToTelegram(command);
125-
//Serial.println(mess);
126-
int messageLenght=mess.length();
127-
for (int m=5; m<messageLenght+1; m++) {
128-
if (mess.substring(m-10,m)=="{\"ok\":true") { //Chek if message has been properly sent
129-
sent=true;
130-
break;
131-
}
132-
}
133-
if (sent==true) {
134-
// Serial.print("Message delivred: \"");
135-
// Serial.print(text);
136-
// Serial.println("\"");
137-
// Serial.println();
138-
break;
139-
}
140-
delay(1000);
141-
// Serial.println("Retry");
142-
143-
}
144-
}
145-
// if (sent==false) Serial.println("Message not delivered");
146-
}
147-
148-
ESP8266TelegramBOT::ESP8266TelegramBOT(String token) :TelegramBOT(token) {
27+
ESP8266TelegramBOT::ESP8266TelegramBOT(String token) :TelegramBotCore(token) {
14928
}
15029

15130
String ESP8266TelegramBOT::sendGetToTelegram(String command) {
@@ -165,7 +44,7 @@ String ESP8266TelegramBOT::sendGetToTelegram(String command) {
16544
while (client.available()) {
16645
char c = client.read();
16746
//Serial.write(c);
168-
if (ch_count < maxBufferSize) {
47+
if (ch_count < maxMessageLength) {
16948
mess=mess+c;
17049
ch_count++;
17150
}

src/ESP8266TelegramBOT.h

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,46 +28,19 @@
2828
#include <ArduinoJson.h>
2929
#include <ESP8266WiFi.h>
3030
#include <WiFiClientSecure.h>
31+
#include <TelegramBotCore.h>
3132

32-
#define HOST "api.telegram.org"
33-
#define SSL_PORT 443
34-
#define HANDLE_MESSAGES 1
33+
#define MAX_BUFFER_SIZE 1000
3534

36-
struct telegramMessage{
37-
String text;
38-
String chat_id;
39-
String sender;
40-
String date;
41-
int update_id;
42-
};
43-
44-
class TelegramBOT
45-
{
46-
public:
47-
TelegramBOT (String);
48-
virtual String sendGetToTelegram(String command) = 0;
49-
void begin(void);
50-
bool getMe();
51-
void sendMessage(String chat_id, String text, String reply_markup);
52-
int getUpdates(int offset);
53-
telegramMessage messages[HANDLE_MESSAGES];
54-
int last_message_recived;
55-
String name;
56-
String userName;
57-
const char* fingerprint = "37:21:36:77:50:57:F3:C9:28:D0:F7:FA:4C:05:35:7F:60:C1:20:44"; //Telegram.org Certificate
58-
59-
private:
60-
String _token;
61-
};
62-
63-
class ESP8266TelegramBOT: public TelegramBOT
35+
class ESP8266TelegramBOT: public TelegramBotCore
6436
{
6537
public:
6638
ESP8266TelegramBOT (String);
6739
String sendGetToTelegram(String command);
6840

6941
private:
7042
WiFiClientSecure client;
43+
const int maxMessageLength = 1000;
7144

7245
};
7346

src/TelegramBotCore.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
2+
/*
3+
Copyright (c) 2015 Giancarlo Bacchio. All right reserved.
4+
5+
TelegramBot - Library to create your own Telegram Bot using
6+
ESP8266 on Arduino IDE.
7+
Ref. Library at https:github/esp8266/Arduino
8+
9+
This library is free software; you can redistribute it and/or
10+
modify it under the terms of the GNU Lesser General Public
11+
License as published by the Free Software Foundation; either
12+
version 2.1 of the License, or (at your option) any later version.
13+
14+
This library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
Lesser General Public License for more details.
18+
19+
You should have received a copy of the GNU Lesser General Public
20+
License along with this library; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
*/
23+
24+
25+
#include "TelegramBotCore.h"
26+
27+
TelegramBotCore::TelegramBotCore(String token) {
28+
_token=token;
29+
}
30+
31+
void TelegramBotCore::begin(void) {
32+
33+
}
34+
35+
bool TelegramBotCore::getMe() {
36+
String command="bot"+_token+"/getMe";
37+
String response = sendGetToTelegram(command); //recieve reply from telegram.org
38+
StaticJsonBuffer<500> jsonBuffer;
39+
JsonObject& root = jsonBuffer.parseObject(response);
40+
if(root.success()) {
41+
if (root.containsKey("result")) {
42+
String _name = root["result"]["first_name"];
43+
String _username = root["result"]["username"];
44+
name = _name;
45+
userName = _username;
46+
return true;
47+
}
48+
}
49+
50+
return false;
51+
}
52+
53+
/***************************************************************
54+
* GetUpdates - function to receive messages from telegram *
55+
* (Argument to pass: the last+1 message to read) *
56+
* Returns the number of new messages *
57+
***************************************************************/
58+
int TelegramBotCore::getUpdates(int offset) {
59+
60+
//Serial.println("GET Update Messages ");
61+
String command="bot"+_token+"/getUpdates?offset="+String(offset)+"&limit="+String(HANDLE_MESSAGES);
62+
String response = sendGetToTelegram(command); //recieve reply from telegram.org
63+
if (response != "") {
64+
// Serial.print("incoming message length");
65+
// Serial.println(response.length());
66+
// Serial.print("Creating StaticJsonBuffer of size: ");
67+
// Serial.println(MAX_BUFFER_SIZE);
68+
StaticJsonBuffer<MAX_BUFFER_SIZE> jsonBuffer;
69+
70+
// Parse response into Json object
71+
JsonObject& root = jsonBuffer.parseObject(response);
72+
if(root.success()) {
73+
// root.printTo(Serial);
74+
// Serial.println();
75+
if (root.containsKey("result")) {
76+
int resultArrayLength = root["result"].size();
77+
if(resultArrayLength > 0) {
78+
int newMessageIndex = 0;
79+
for(int i=0; i < resultArrayLength; i++){
80+
int update_id = root["result"][i]["update_id"];
81+
if(last_message_recived != update_id) {
82+
last_message_recived = update_id;
83+
String text = root["result"][i]["message"]["text"];
84+
String date = root["result"][i]["message"]["date"];
85+
String chat_id = root["result"][i]["message"]["chat"]["id"];
86+
87+
messages[newMessageIndex].update_id = update_id;
88+
messages[newMessageIndex].text = text;
89+
messages[newMessageIndex].date = date;
90+
messages[newMessageIndex].chat_id = chat_id;
91+
92+
newMessageIndex++;
93+
}
94+
}
95+
return newMessageIndex;
96+
} else {
97+
//Serial.println("no new messages");
98+
}
99+
} else {
100+
Serial.println("Response contained no 'result'");
101+
}
102+
} else {
103+
// Buffer may not be big enough, increase buffer or reduce max number of messages
104+
Serial.println("Failed to parse update");
105+
}
106+
107+
return 0;
108+
}
109+
}
110+
111+
/***********************************************************************
112+
* SendMessage - function to send message to telegram *
113+
* (Arguments to pass: chat_id, text to transmit and markup(optional)) *
114+
***********************************************************************/
115+
void TelegramBotCore::sendMessage(String chat_id, String text, String reply_markup) {
116+
117+
bool sent=false;
118+
// Serial.println("SEND Message ");
119+
long sttime=millis();
120+
if (text!="") {
121+
while (millis()<sttime+8000) { // loop for a while to send the message
122+
String command="bot"+_token+"/sendMessage?chat_id="+chat_id+"&text="+text+"&reply_markup="+reply_markup;
123+
String mess=sendGetToTelegram(command);
124+
//Serial.println(mess);
125+
int messageLenght=mess.length();
126+
for (int m=5; m<messageLenght+1; m++) {
127+
if (mess.substring(m-10,m)=="{\"ok\":true") { //Chek if message has been properly sent
128+
sent=true;
129+
break;
130+
}
131+
}
132+
if (sent==true) {
133+
// Serial.print("Message delivred: \"");
134+
// Serial.print(text);
135+
// Serial.println("\"");
136+
// Serial.println();
137+
break;
138+
}
139+
delay(1000);
140+
// Serial.println("Retry");
141+
142+
}
143+
}
144+
// if (sent==false) Serial.println("Message not delivered");
145+
}

src/TelegramBotCore.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright (c) 2015 Giancarlo Bacchio. All right reserved.
3+
4+
TelegramBot - Library to create your own Telegram Bot using
5+
ESP8266 on Arduino IDE.
6+
Ref. Library at https:github/esp8266/Arduino
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License as published by the Free Software Foundation; either
11+
version 2.1 of the License, or (at your option) any later version.
12+
13+
This library is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
Lesser General Public License for more details.
17+
18+
You should have received a copy of the GNU Lesser General Public
19+
License along with this library; if not, write to the Free Software
20+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
23+
24+
#ifndef TelegramBotCore_h
25+
#define TelegramBotCore_h
26+
27+
#include <Arduino.h>
28+
#include <ArduinoJson.h>
29+
30+
#define HOST "api.telegram.org"
31+
#define SSL_PORT 443
32+
#define HANDLE_MESSAGES 1
33+
#define MAX_BUFFER_SIZE 1000
34+
35+
struct telegramMessage{
36+
String text;
37+
String chat_id;
38+
String sender;
39+
String date;
40+
int update_id;
41+
};
42+
43+
class TelegramBotCore
44+
{
45+
public:
46+
TelegramBotCore (String);
47+
virtual String sendGetToTelegram(String command) = 0;
48+
void begin(void);
49+
bool getMe();
50+
void sendMessage(String chat_id, String text, String reply_markup);
51+
int getUpdates(int offset);
52+
telegramMessage messages[HANDLE_MESSAGES];
53+
int last_message_recived;
54+
String name;
55+
String userName;
56+
const char* fingerprint = "37:21:36:77:50:57:F3:C9:28:D0:F7:FA:4C:05:35:7F:60:C1:20:44"; //Telegram.org Certificate
57+
58+
private:
59+
String _token;
60+
};
61+
62+
#endif

0 commit comments

Comments
 (0)