-
Notifications
You must be signed in to change notification settings - Fork 0
/
Telegram.cpp
74 lines (62 loc) · 1.94 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
/**
* @file Telegram.cpp
* @brief A minimal wrapper for sending and receiving messages from the Telegram API using an ESP32 board.
*
* A Simple way to send and receive messages from Telegram using an ESP32 board with internet connectivity.
*
* @author Eetu T. (DeBoogie)
* @date 20/02/2023
*
* @see https://core.telegram.org/bots/api
*/
#include "Telegram.h"
using namespace std;
namespace Telegram
{
HTTPClient http;
int _offset = 0;
Message::Message(String text, String chat_id)
{
this->text = text;
this->chat_id = chat_id;
}
Telegram::Telegram(String token)
{
_token = token;
}
void Telegram::sendMessage(String chat_id, String text)
{
String url = _url + _token + "/sendMessage?chat_id=" + chat_id + "&text=" + text;
_sendRequest(url);
}
String Telegram::_sendRequest(String url)
{
http.begin(url);
int httpCode = http.GET();
String payload = http.getString();
http.end();
return payload;
}
vector<Message> Telegram::readMessages(int timeout, int limit)
{
String url = _url + _token + "/getUpdates?timeout=" + timeout + "&limit=" + limit + "&offset=" + _offset;
String response = _sendRequest(url);
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
JsonObject obj = doc.as<JsonObject>();
vector<Message> messages;
if (obj["ok"] == true)
{
JsonArray result = obj["result"].as<JsonArray>();
for (JsonObject update : result)
{
JsonObject message = update["message"].as<JsonObject>();
String text = message["text"].as<String>();
String chat_id = message["chat"]["id"].as<String>();
_offset = update["update_id"].as<int>() + 1;
messages.push_back(Message(text, chat_id));
}
}
return messages;
};
};