Telegram Bot Api Client  0.6.1
Client_for_accessing_Telegram's_Bot_API
TelegramBotClient.h
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef TelegramBotClient_h
14 #define TelegramBotClient_h
15 
16 #include "TBCDebug.h"
17 #include "Arduino.h"
18 #include <Client.h>
19 #include <ArduinoJson.h>
20 #include "JsonWebClient.h"
21 
22 #define TELEGRAMHOST F("api.telegram.org")
23 #define TELEGRAMPORT 443
24 #define POLLINGTIMEOUT 600
25 #define USERAGENTSTRING F("telegrambotclient /0.1")
26 
27 // Inspired by PubSubClient by Nick O'Leary (http://knolleary.net)
28 #ifdef ESP8266
29 #include <functional>
30 #define TBC_CALLBACK_RECEIVE_SIGNATURE std::function<void(TelegramProcessError, JwcProcessError, Message*)> callbackReceive
31 #define TBC_CALLBACK_ERROR_SIGNATURE std::function<void(TelegramProcessError, JwcProcessError)> callbackError
32 #else
33 #define TBC_CALLBACK_RECEIVE_SIGNATURE void (*callbackReceive)(TelegramProcessError, JwcProcessError, Message*)
34 #define TBC_CALLBACK_ERROR_SIGNATURE void (*callbackError)(TelegramProcessError, JwcProcessError)
35 #endif
36 
53 enum class TelegramProcessError : int
54 {
56  Ok = 0,
58  JcwPollErr = -1,
60  JcwPostErr = -2,
62  RetPollErr = -3,
64  RetPostErr = -4
65 };
66 
68 static String TelegramProcessErrorString[] = {"Ok", "JcwPollErr", "JcwPostErr", "RetPollErr", "RetPostErr"};
69 
70 static String toString(TelegramProcessError err)
71 {
72  return TelegramProcessErrorString[(-1) * (int) err];
73 }
74 
93 struct Message {
101  long UpdateId;
104  long MessageId;
107  long FromId;
110  bool FromIsBot;
117  String FromLastName;
129  long ChatId;
135  String ChatLastName;
138  String ChatType;
142  String Text;
146  long Date;
147 };
148 
165 {
166  uint Count;
167  String* Buttons;
168 };
169 
188 {
189  private:
190 
191  uint Count; /*< Maximum number of rows in the keyboard */
192  uint Counter; /*< Number of rows already stored in keyboard */
193  TBCKeyBoardRow* Rows; /*< Array of rows in the keyboard */
203  bool OneTime = false;
213  bool Resize = false;
214  public:
222  TBCKeyBoard (uint count, bool oneTime = false, bool resize = false);
227  ~TBCKeyBoard ();
238  TBCKeyBoard& push(uint count, const String buttons[]);
248  const String get(const uint row, const uint col);
258  const int length (const uint row);
267  const int length ();
268 
276  const bool getOneTime() {
277  return OneTime;
278  }
286  const bool getResize() {
287  return Resize;
288  }
289 
290 };
291 
308 {
309  private:
313  long LastUpdateId = 0;
315  String Token;
319  bool Parallel = false;
326 
334  void startPolling();
344  void startPosting(String Message);
349  public:
363  String token,
364  Client& sslPollClient,
365  Client& sslPostClient,
368  );
378  String token,
379  Client& sslPollClient,
380  Client& sslPostClient)
381  : TelegramBotClient (token, sslPollClient, sslPostClient, 0, 0) {};
391  String token,
392  Client& sslPollClient)
393  : TelegramBotClient (token, sslPollClient, sslPollClient, 0, 0) {};
412  void begin(
426  void setCallbacks(
429 
437  bool loop();
449  void postMessage(long chatId, String text, TBCKeyBoard& keyBoard);
461  void postMessage(long chatId, String text) {TBCKeyBoard keyBoard(0);
462  postMessage(chatId, text, keyBoard);
463  }
475  void pollSuccess(JwcProcessError err, JsonObject& json);
487  void pollError(JwcProcessError err, Client* client);
499  void postSuccess(JwcProcessError err, JsonObject& json);
511  void postError(JwcProcessError err, Client* client);
512 
513  static void callbackPollSuccess (void* obj, JwcProcessError err, JsonObject& json)
514  {
515  if (obj == 0) return;
516  TelegramBotClient* botClient = (TelegramBotClient*)obj;
517  botClient->pollSuccess(err, json);
518  }
519  static void callbackPollError(void* obj, JwcProcessError err, Client* client)
520  {
521  if (obj == 0) return;
522  TelegramBotClient* botClient = (TelegramBotClient*)obj;
523  botClient->pollError(err, client);
524  }
525  static void callbackPostSuccess (void* obj, JwcProcessError err, JsonObject& json)
526  {
527  if (obj == 0) return;
528  TelegramBotClient* botClient = (TelegramBotClient*)obj;
529  botClient->postSuccess(err, json);
530  }
531  static void callbackPostError(void* obj, JwcProcessError err, Client* client)
532  {
533  if (obj == 0) return;
534  TelegramBotClient* botClient = (TelegramBotClient*)obj;
535  botClient->postError(err, client);
536  }
537 };
538 
539 #endif
540 
541 
542 
Definition: JsonWebClient.h:136
long ChatId
Definition: TelegramBotClient.h:129
TBCKeyBoard & push(uint count, const String buttons[])
Adds a row to the keyboard.
Definition: TelegramBotClient.cpp:292
Definition: TelegramBotClient.h:164
Definition: TelegramBotClient.h:307
String Text
Definition: TelegramBotClient.h:142
Definition: TelegramBotClient.h:187
~TelegramBotClient()
Destructor.
Definition: TelegramBotClient.cpp:25
TBCKeyBoard(uint count, bool oneTime=false, bool resize=false)
Constructor.
Definition: TelegramBotClient.cpp:269
bool loop()
Handles client background tasks.
Definition: TelegramBotClient.cpp:50
void startPosting(String Message)
Starts posting a message.
Definition: TelegramBotClient.cpp:184
TelegramBotClient(String token, Client &sslPollClient, Client &sslPostClient, TBC_CALLBACK_RECEIVE_SIGNATURE, TBC_CALLBACK_ERROR_SIGNATURE)
Constructor.
Definition: TelegramBotClient.cpp:3
long FromId
Definition: TelegramBotClient.h:107
String Token
Definition: TelegramBotClient.h:315
void pollError(JwcProcessError err, Client *client)
Callback called by JSONWebClient.
Definition: TelegramBotClient.cpp:150
String ChatFirstName
Definition: TelegramBotClient.h:132
Definition: TelegramBotClient.h:93
void setCallbacks(TBC_CALLBACK_RECEIVE_SIGNATURE, TBC_CALLBACK_ERROR_SIGNATURE)
Sets callbacks.
Definition: TelegramBotClient.cpp:31
void postMessage(long chatId, String text)
Post a message.
Definition: TelegramBotClient.h:461
JsonWebClient * SslPollClient
Definition: TelegramBotClient.h:321
const int length()
Length of keyboard.
Definition: TelegramBotClient.cpp:319
~TBCKeyBoard()
Destructor.
Definition: TelegramBotClient.cpp:282
JsonWebClient * SslPostClient
Definition: TelegramBotClient.h:325
long LastUpdateId
Definition: TelegramBotClient.h:313
String FromLanguageCode
Definition: TelegramBotClient.h:121
String ChatType
Definition: TelegramBotClient.h:138
String FromFirstName
Definition: TelegramBotClient.h:114
TelegramBotClient(String token, Client &sslPollClient)
Constructor.
Definition: TelegramBotClient.h:390
void postSuccess(JwcProcessError err, JsonObject &json)
Callback called by JSONWebClient.
Definition: TelegramBotClient.cpp:254
long MessageId
Definition: TelegramBotClient.h:104
bool FromIsBot
Definition: TelegramBotClient.h:110
const bool getOneTime()
Gets value of OneTime.
Definition: TelegramBotClient.h:276
const bool getResize()
Gets value of Resize.
Definition: TelegramBotClient.h:286
void postMessage(long chatId, String text, TBCKeyBoard &keyBoard)
Post a message.
Definition: TelegramBotClient.cpp:211
void begin(TBC_CALLBACK_RECEIVE_SIGNATURE, TBC_CALLBACK_ERROR_SIGNATURE)
Alias for setCallbacks following Arduino convention.
Definition: TelegramBotClient.cpp:40
String FromLastName
Definition: TelegramBotClient.h:117
String ChatLastName
Definition: TelegramBotClient.h:135
bool Parallel
Definition: TelegramBotClient.h:319
bool OneTime
Definition: TelegramBotClient.h:203
long UpdateId
Definition: TelegramBotClient.h:101
TelegramBotClient(String token, Client &sslPollClient, Client &sslPostClient)
Constructor.
Definition: TelegramBotClient.h:377
bool Resize
Definition: TelegramBotClient.h:213
TBC_CALLBACK_RECEIVE_SIGNATURE
Definition: TelegramBotClient.h:346
void pollSuccess(JwcProcessError err, JsonObject &json)
Callback called by JSONWebClient.
Definition: TelegramBotClient.cpp:98
void startPolling()
Starts polling.
Definition: TelegramBotClient.cpp:68
TBC_CALLBACK_ERROR_SIGNATURE
Definition: TelegramBotClient.h:348
void postError(JwcProcessError err, Client *client)
Callback called by JSONWebClient.
Definition: TelegramBotClient.cpp:259
long Date
Definition: TelegramBotClient.h:146
Header of a simple web client receiving json uses an underlying implementation of Client interface...