-
Notifications
You must be signed in to change notification settings - Fork 26
/
UniversalTelegramBot.h
154 lines (130 loc) · 5.83 KB
/
UniversalTelegramBot.h
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright (c) 2018 Brian Lough. All right reserved.
UniversalTelegramBot - Library to create your own Telegram Bot using
ESP8266 or ESP32 on Arduino IDE.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef UniversalTelegramBot_h
#define UniversalTelegramBot_h
//#define TELEGRAM_DEBUG 0 //jz
#define ARDUINOJSON_DECODE_UNICODE 1
#define ARDUINOJSON_USE_LONG_LONG 1
#include <Arduino.h>
#include <ArduinoJson.h>
#include <Client.h>
#define TELEGRAM_HOST "api.telegram.org"
#define TELEGRAM_SSL_PORT 443
#define HANDLE_MESSAGES 1
//unmark following line to enable debug mode
//#define _debug
typedef bool (*MoreDataAvailable)();
typedef byte (*GetNextByte)();
typedef byte* (*GetNextBuffer)();
typedef int (GetNextBufferLen)();
struct telegramMessage {
String text;
String chat_id;
String chat_title;
String from_id;
String from_name;
String date;
String type;
String file_caption;
String file_path;
String file_name;
bool hasDocument;
long file_size;
float longitude;
float latitude;
int update_id;
int reply_to_message_id;
String reply_to_text;
String query_id;
};
class UniversalTelegramBot {
public:
UniversalTelegramBot(const String& token, Client &client);
void updateToken(const String& token);
String getToken();
String sendGetToTelegram(const String& command);
String sendPostToTelegram(const String& command, JsonObject payload);
String
sendMultipartFormDataToTelegram(const String& command, const String& binaryPropertyName,
const String& fileName, const String& contentType,
const String& chat_id, int fileSize,
MoreDataAvailable moreDataAvailableCallback,
GetNextByte getNextByteCallback,
GetNextBuffer getNextBufferCallback,
GetNextBufferLen getNextBufferLenCallback);
//jz caption
String
sendMultipartFormDataToTelegramWithCaption(const String& command, const String& binaryPropertyName,
const String& fileName, const String& contentType,
const String& caption,
const String& chat_id, int fileSize,
MoreDataAvailable moreDataAvailableCallback,
GetNextByte getNextByteCallback,
GetNextBuffer getNextBufferCallback,
GetNextBufferLen getNextBufferLenCallback);
bool readHTTPAnswer(String &body, String &headers);
bool getMe();
bool sendSimpleMessage(const String& chat_id, const String& text, const String& parse_mode);
bool sendMessage(const String& chat_id, const String& text, const String& parse_mode = "");
bool sendMessageWithReplyKeyboard(const String& chat_id, const String& text,
const String& parse_mode, const String& keyboard,
bool resize = false, bool oneTime = false,
bool selective = false);
bool sendMessageWithInlineKeyboard(const String& chat_id, const String& text,
const String& parse_mode, const String& keyboard);
bool sendChatAction(const String& chat_id, const String& text);
bool sendPostMessage(JsonObject payload);
String sendPostPhoto(JsonObject payload);
String sendPhotoByBinary(const String& chat_id, const String& contentType, int fileSize,
MoreDataAvailable moreDataAvailableCallback,
GetNextByte getNextByteCallback,
GetNextBuffer getNextBufferCallback,
GetNextBufferLen getNextBufferLenCallback);
String sendPhoto(const String& chat_id, const String& photo, const String& caption = "",
bool disable_notification = false,
int reply_to_message_id = 0, const String& keyboard = "");
bool answerCallbackQuery(const String &query_id,
const String &text = "",
bool show_alert = false,
const String &url = "",
int cache_time = 0);
bool setMyCommands(const String& commandArray);
String buildCommand(const String& cmd);
int getUpdates(long offset);
bool checkForOkResponse(const String& response);
telegramMessage messages[HANDLE_MESSAGES];
long last_message_received;
String name;
String userName;
int longPoll = 0;
int waitForResponse = 1500;
int _lastError;
int last_sent_message_id = 0;
int maxMessageLength = 1500;
int jzdelay = 0; // delay between multipart blocks
//int jzblocksize = 32 * 512;
#define jzblocksize 32 * 512
byte buffer[jzblocksize];
private:
// JsonObject * parseUpdates(String response);
String _token;
Client *client;
void closeClient();
bool getFile(String& file_path, long& file_size, const String& file_id);
bool processResult(JsonObject result, int messageIndex);
};
#endif