-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.cpp
118 lines (97 loc) · 3 KB
/
telegram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ThePirat 2023 - Telegram helper using UniversalTelegramBot library v1.3.0*/
#include "telegram.h"
unsigned long buffer_pos = 0;
unsigned long buffer_length = 0;
const unsigned long CHUNK_SIZE = 8000UL;
uint8_t *imageBuffer;
unsigned long imageLen;
Telegram::Telegram(const String& token, const String& defaultChatId, long message_id, HandleMessage handleMessageCallback)
{
this->handleMessageCallback = handleMessageCallback;
_token = token;
_defaultChatId = defaultChatId;
this->client = new WiFiClientSecure();
this->client->setInsecure();
this->bot = new UniversalTelegramBot(token, *this->client);
this->bot->last_message_received = message_id;
}
void Telegram::ProcessInputMessages()
{
Serial.println("Will process telegram messages. Offset: " + String(bot->last_message_received));
int numNewMessages = bot->getUpdates(bot->last_message_received + 1);
while (numNewMessages)
{
handleNewMessages(numNewMessages);
numNewMessages = bot->getUpdates(bot->last_message_received + 1);
}
Serial.println("Telegram messages processing completed.");
}
void Telegram::SendMessage(const String& text)
{
Serial.println("Sending message to telegram: " + text);
bot->sendMessage(getChatId(), text, "markdown");
}
void Telegram::SendMessageWithReplyKeyboard(const String& text, const String& jsonKeyboard)
{
Serial.println("Sending keyboard message to telegram: " + text);
bot->sendMessageWithReplyKeyboard(getChatId(), text, "markdown", jsonKeyboard, true);
}
void Telegram::SendImage(uint8_t *buffer, unsigned long len)
{
imageBuffer = buffer;
imageLen = len;
buffer_pos = 0;
Serial.println("Sending image to telegram, Len: " + String(imageLen));
bot->sendPhotoByBinary(getChatId(), "image/jpeg", imageLen, isMoreDataAvailable, nullptr, getNextBuffer, getNextBufferLen);
}
bool Telegram::SetCommands(const String& commands)
{
return bot->setMyCommands(commands);
}
void Telegram::handleNewMessages(int numNewMessages)
{
for (int i = 0; i < numNewMessages; i++)
{
String chat_id = bot->messages[i].chat_id;
String text = bot->messages[i].text;
String from_name = bot->messages[i].from_name;
Serial.println("got message: '" + text + "' from: " + from_name + " chat id: " + String(chat_id));
_lastChatId = chat_id;
this->handleMessageCallback(text, chat_id, from_name, bot->last_message_received);
}
}
String Telegram::getChatId()
{
return _lastChatId.length() > 0 ? _lastChatId : _defaultChatId;
}
/* Send image callbacks */
bool isMoreDataAvailable()
{
bool moreData = imageBuffer && buffer_pos < imageLen;
return moreData;
}
byte *getNextBuffer()
{
if (imageBuffer && buffer_pos < imageLen)
{
byte* nextBuffer = &imageBuffer[buffer_pos];
buffer_length = min(CHUNK_SIZE, imageLen - buffer_pos);
buffer_pos += buffer_length;
return nextBuffer;
}
else
{
return nullptr;
}
}
int getNextBufferLen()
{
if (imageBuffer)
{
return buffer_length;
}
else
{
return 0;
}
}