5#define ARDUINOJSON_USE_LONG_LONG 1
6#define ARDUINOJSON_DECODE_UNICODE 1
7#include <ArduinoJson.h>
9#if defined(ESP32) || defined(ESP8266)
10#define FS_SUPPORT true
12#include <WiFiClientSecure.h>
14#define FS_SUPPORT false
28#if defined(ESP32) || defined(ESP8266)
29#define FS_SUPPORT true
31#include <WiFiClientSecure.h>
33#define FS_SUPPORT false
38#if INT_MAX == 2147483647
49#define MAX_INLINEKYB_CB 30
51#define SERVER_TIMEOUT 10000
52#define MIN_UPDATE_TIME 500
54#define BLOCK_SIZE 1436
61#define TELEGRAM_HOST "api.telegram.org"
62#define TELEGRAM_IP "149.154.167.220"
63#define TELEGRAM_PORT 443
66static const char telegram_cert[] PROGMEM = R
"EOF(
67-----BEGIN CERTIFICATE-----
68MIIEADCCAuigAwIBAgIBADANBgkqhkiG9w0BAQUFADBjMQswCQYDVQQGEwJVUzEh
69MB8GA1UEChMYVGhlIEdvIERhZGR5IEdyb3VwLCBJbmMuMTEwLwYDVQQLEyhHbyBE
70YWRkeSBDbGFzcyAyIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTA0MDYyOTE3
71MDYyMFoXDTM0MDYyOTE3MDYyMFowYzELMAkGA1UEBhMCVVMxITAfBgNVBAoTGFRo
72ZSBHbyBEYWRkeSBHcm91cCwgSW5jLjExMC8GA1UECxMoR28gRGFkZHkgQ2xhc3Mg
73MiBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTCCASAwDQYJKoZIhvcNAQEBBQADggEN
74ADCCAQgCggEBAN6d1+pXGEmhW+vXX0iG6r7d/+TvZxz0ZWizV3GgXne77ZtJ6XCA
75PVYYYwhv2vLM0D9/AlQiVBDYsoHUwHU9S3/Hd8M+eKsaA7Ugay9qK7HFiH7Eux6w
76wdhFJ2+qN1j3hybX2C32qRe3H3I2TqYXP2WYktsqbl2i/ojgC95/5Y0V4evLOtXi
77EqITLdiOr18SPaAIBQi2XKVlOARFmR6jYGB0xUGlcmIbYsUfb18aQr4CUWWoriMY
78avx4A6lNf4DD+qta/KFApMoZFv6yyO9ecw3ud72a9nmYvLEHZ6IVDd2gWMZEewo+
79YihfukEHU1jPEX44dMX4/7VpkI+EdOqXG68CAQOjgcAwgb0wHQYDVR0OBBYEFNLE
80sNKR1EwRcbNhyz2h/t2oatTjMIGNBgNVHSMEgYUwgYKAFNLEsNKR1EwRcbNhyz2h
81/t2oatTjoWekZTBjMQswCQYDVQQGEwJVUzEhMB8GA1UEChMYVGhlIEdvIERhZGR5
82IEdyb3VwLCBJbmMuMTEwLwYDVQQLEyhHbyBEYWRkeSBDbGFzcyAyIENlcnRpZmlj
83YXRpb24gQXV0aG9yaXR5ggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQAD
84ggEBADJL87LKPpH8EsahB4yOd6AzBhRckB4Y9wimPQoZ+YeAEW5p5JYXMP80kWNy
85OO7MHAGjHZQopDH2esRU1/blMVgDoszOYtuURXO1v0XJJLXVggKtI3lpjbi2Tc7P
86TMozI+gciKqdi0FuFskg5YmezTvacPd+mSYgFFQlq25zheabIZ0KbIIOqPjCDPoQ
87HmyW74cNxA9hi63ugyuV+I6ShHI56yDqg+2DzZduCLzrTia2cyvk0/ZM/iZx4mER
88dEr/VxqHD3VILs9RaRegAhJhldXRQLIQTO7ErBBDpqWeCtWVYpoNz4iCxTIM5Cuf
89ReYNnyicsbkqWletNw+vHX/bvZ8=
90-----END CERTIFICATE-----
97 typedef void(*SentCallback)(
bool sent);
151 bool sendMessage(
const TBMessage &msg,
const char *message,
const char *keyboard =
nullptr,
bool wait =
false);
156 return sendMessage(msg, message.c_str(), keyboard.c_str());
173 bool sendToChannel(
const char *channel,
const char *message,
bool silent =
false);
175 inline bool sendToChannel(
const String &channel,
const String &message,
bool silent)
177 return sendToChannel(channel.c_str(), message.c_str(), silent);
183 inline bool sendTo(
const int64_t userid,
const char *message,
const char *keyboard =
nullptr)
190 inline bool sendTo(
const int64_t userid,
const String &message, String keyboard =
"")
192 return sendTo(userid, message.c_str(), keyboard.c_str());
208 bool sendDocument(int64_t chat_id, Stream &stream,
size_t size,
209 DocumentType doc,
const char *filename,
const char *caption =
nullptr);
212 DocumentType doc,
const char *filename,
const char *caption =
nullptr)
218 bool sendPhotoByUrl(
const int64_t &chat_id,
const char *url,
const char *caption);
221 bool sendAnimationByUrl(
const int64_t &chat_id,
const char *url,
const char *caption);
223 inline bool sendPhoto(
const int64_t &chat_id,
const char *url,
const char *caption)
228 inline bool sendPhoto(
const int64_t &chat_id,
const String &url,
const String &caption)
239 inline bool sendPhoto(int64_t chat_id, Stream &stream,
size_t size,
const char *filename =
nullptr,
const char *caption =
nullptr)
241 return sendStream(chat_id,
"sendPhoto",
"image/jpeg",
"photo", stream, size, filename, caption);
244 inline bool sendPhoto(
const TBMessage &msg, Stream &stream,
size_t size,
const char *filename,
const char *caption =
nullptr)
246 return sendStream(msg.
chatId,
"sendPhoto",
"image/jpeg",
"photo", stream, size, filename, caption);
249#if FS_SUPPORT == true
251 inline bool sendPhoto(int64_t chat_id,
const char *filename, fs::FS &fs,
const char *caption =
nullptr)
253 File file = fs.open(filename,
"r");
254 bool res = sendStream(chat_id,
"sendPhoto",
"image/jpeg",
"photo", file, file.size(), file.name(), caption);
258 inline bool sendPhoto(
const TBMessage &msg,
const char *filename, fs::FS &fs,
const char *caption =
nullptr)
260 File file = fs.open(filename,
"r");
261 bool res = sendStream(msg.
chatId,
"sendPhoto",
"image/jpeg",
"photo", file, file.size(), file.name(), caption);
268 inline bool sendPhoto(int64_t chat_id, uint8_t *data,
size_t size,
const char *caption =
nullptr)
270 return sendBuffer(chat_id,
"sendPhoto",
"image/jpeg",
"photo", data, size, caption);
273 inline bool sendPhoto(
const TBMessage &msg, uint8_t *data,
size_t size,
const char *caption =
nullptr)
275 return sendBuffer(msg.
chatId,
"sendPhoto",
"image/jpeg",
"photo", data, size, caption);
280 inline bool sendPhotoByUrl(
const int64_t &chat_id,
const String &url,
const String &caption)
292 return sendStream(chat_id,
"sendPhoto",
"image/jpeg",
"photo", *stream, size,
nullptr,
nullptr);
295#if FS_SUPPORT == true
296 inline bool sendPhotoByFile(int64_t chat_id,
const char *filename, fs::FS &fs)
298 File file = fs.open(filename,
"r");
299 Serial.println(file.size());
300 bool res = sendStream(chat_id,
"sendPhoto",
"image/jpeg",
"photo", file, file.size(), file.name(),
nullptr);
316 bool endQuery(
const TBMessage &msg,
const char *message,
bool alertMode =
false);
334 return m_botusername.c_str();
358 m_keyboards[m_keyboardCount++] = keyb;
387 bool editMessage(int32_t chat_id, int32_t message_id,
const String &txt,
const String &keyboard);
412 if (sentcb !=
nullptr)
414 m_sentCallback = sentcb;
415 m_sentTimeout = timeout;
430 m_formatType = format;
434 m_JsonBufferSize = jsonBufferSize;
438 Client *telegramClient;
441 String m_botusername;
443 int32_t m_lastUpdateId = 0;
444 uint32_t m_lastUpdateTime;
447 uint32_t m_lastmsg_timestamp;
451 uint8_t m_keyboardCount = 0;
453 void setformData(int64_t chat_id,
const char *cmd,
const char *type,
const char *propName,
size_t size,
454 String &formData, String &request,
const char *filename,
const char *caption);
455 bool sendStream(int64_t chat_id,
const char *command,
const char *contentType,
const char *binaryPropertyName,
456 Stream &stream,
size_t size,
const char *filename,
const char *caption);
457 bool sendBuffer(int64_t chat_id,
const char *cmd,
const char *type,
const char *propName, uint8_t *data,
size_t size,
const char *caption);
459 SentCallback m_sentCallback =
nullptr;
460 bool m_waitSent =
false;
461 uint32_t m_sentTimeout;
462 uint32_t m_lastSentTime;
463 uint32_t m_lastSentMsgId;
465 uint32_t testReconnectTime;
467 uint8_t m_formatType =
HTML;
479 bool sendCommand(
const char *command,
const char *payload,
bool blocking =
false);
bool setMyCommands(const String &cmd, const String &desc)
bool sendAnimationByUrl(const int64_t &chat_id, const char *url, const char *caption)
void getMyCommands(String &cmdList)
bool sendPhoto(int64_t chat_id, uint8_t *data, size_t size, const char *caption=nullptr)
bool sendPhotoByUrl(const int64_t &chat_id, const String &url, const String &caption)
bool sendPhoto(const int64_t &chat_id, const String &url, const String &caption)
bool getFile(TBDocument &doc)
bool sendTo(const int64_t userid, const char *message, const char *keyboard=nullptr)
void addInlineKeyboard(InlineKeyboard *keyb)
int64_t getGroupId(const TBMessage &msg)
bool endQuery(const TBMessage &msg, const char *message, bool alertMode=false)
void setJsonBufferSize(uint32_t jsonBufferSize)
bool sendPhotoByUrl(const int64_t &chat_id, const char *url, const char *caption)
void addSentCallback(SentCallback sentcb, uint32_t timeout=1000)
const char * getBotName()
bool sendDocument(int64_t chat_id, Stream &stream, size_t size, DocumentType doc, const char *filename, const char *caption=nullptr)
MessageType getNewMessage(TBMessage &message)
bool sendCommand(const char *command, const char *payload, bool blocking=false)
bool sendMessage(const TBMessage &msg, const char *message, const char *keyboard=nullptr, bool wait=false)
bool sendPhotoByFile(int64_t chat_id, Stream *stream, size_t size)
bool sendMessage(const TBMessage &msg, const String &message, String keyboard="")
void setFormattingStyle(uint8_t format)
bool sendMessage(const TBMessage &msg, const char *message, InlineKeyboard &keyboard)
bool sendPhoto(const TBMessage &msg, const String &url, const String &caption)
bool sendToChannel(const char *channel, const char *message, bool silent=false)
void setTelegramToken(const char *token)
bool sendToChannel(const String &channel, const String &message, bool silent)
bool sendMessage(const TBMessage &msg, const char *message, ReplyKeyboard &keyboard)
bool editMessage(int32_t chat_id, int32_t message_id, const String &txt, InlineKeyboard &keyboard)
bool sendTo(const int64_t userid, const String &message, String keyboard="")
void setUpdateTime(uint32_t pollingTime)
bool editMessage(const TBMessage &msg, const String &txt, const String &keyboard)
bool sendPhotoByUrl(const TBMessage &msg, const String &url, const String &caption)
bool removeReplyKeyboard(const TBMessage &msg, const char *message, bool selective=false)
bool sendPhoto(const int64_t &chat_id, const char *url, const char *caption)
bool sendPhoto(const TBMessage &msg, uint8_t *data, size_t size, const char *caption=nullptr)
bool sendPhoto(int64_t chat_id, Stream &stream, size_t size, const char *filename=nullptr, const char *caption=nullptr)
bool editMessage(const TBMessage &msg, const String &txt, InlineKeyboard &keyboard)
bool forwardMessage(const TBMessage &msg, const int64_t to_chatid)
bool sendPhoto(const TBMessage &msg, Stream &stream, size_t size, const char *filename, const char *caption=nullptr)
bool sendDocument(const TBMessage &msg, Stream &stream, size_t size, DocumentType doc, const char *filename, const char *caption=nullptr)
bool editMessage(int32_t chat_id, int32_t message_id, const String &txt, const String &keyboard)
String getJSON(void) const
String getJSON(void) const