Here you can find an explanation of the functionalities provided and how to use the library. Check the examples folder for demos and examples.
- Introduction and quick start
- Inline Keyboards
- Data types
- Enumerators
- Basic methods
- CTBot::wifiConnect()
- CTBot::setTelegramToken()
- CTBot::setIP()
- CTBot::testConnection()
- CTBot::getNewMessage()
- CTBot::sendMessage()
- CTBot::endQuery()
- CTBot::removeReplyKeyboard()
- CTBotInlineKeyboard::addButton()
- CTBotInlineKeyboard::addRow()
- CTBotInlineKeyboard::flushData()
- CTBotInlineKeyboard::getJSON()
- Configuration methods
Once installed the library, you have to load it in your sketch...
#include "CTBot.h"
...and instantiate a CTBot
object
CTBot myBot;
You can connect to an Access Point by using the wiFiConnect()
member function...
myBot.wifiConnect("mySSID", "myPassword");
...and use the setTelegramToken()
member function to set your Telegram Bot token in order establish connections with the bot
myBot.setTelegramToken("myTelegramBotToken");
In order to receive messages, declare a TBMessage
variable...
TBMessage msg;
...and execute the getNewMessage()
member fuction.
The getNewMessage()
return a non-zero value if there is a new message and store it in the msg
variable. See the TBMessage data type for further details.
myBot.getNewMessage(msg);
To send a message to a Telegram user, use the sendMessage()
member function
myBot.sendMessage(telegramUserID,"message");
See the echoBot example for further details.
The Inline Keyboards are special keyboards integrated directly into the messages they belong to: pressing buttons on inline keyboards doesn't result in messages sent to the chat. Instead, inline keyboards support buttons that work behind the scenes. CTBot class implements the following buttons:
- URL buttons: these buttons have a small arrow icon to help the user understand that tapping on a URL button will open an external link. A confirmation alert message is shown before opening the link in the browser.
- Callback buttons: when a user presses a callback button, no messages are sent to the chat. Instead, the bot simply receives the relevant query. Upon receiving the query, the bot can display some result in a notification at the top of the chat screen or in an alert.
In order to show an inline keyboard, use the method sendMessage() specifing the parameter keyboard
.
The keyboard
parameter is a string that contains a JSON structure that define the inline keyboard. See Telegram docs.
To simplify the creation of an inline keyboard, there is an helper class called CTBotInlineKeyboard
.
Creating an inline keyboard with a CTBotInlineKeyboard
is straightforward:
Fristly, instantiate a CTBotInlineKeyboard
object:
CTBotInlineKeyboard kbd;
then add new buttons in the first row of the inline keyboard using the member fuction addButton()
(See addButton() member function).
kbd.addButton("First Button label", "URL for first button", CTBotKeyboardButtonURL); // URL button
kbd.addButton("Second Button label", "Data for second button", CTBotKeyboardButtonQuery); // callback button
...
If a new row of buttons is needed, call the addRow() member function...
kbd.addRow();
... and add buttons to the just created row:
kbd.addButton("New Row Button label", "URL for the new row button", CTBotKeyboardButtonURL); // URL button
...
Once finished, send the inline keyboard using the sendMessage
method:
myBot.sendMessage(<telegramUserID>, "message", kbd);
...
Everytime an inline keyboard button is pressed, a special message is sent to the bot: the getNewMessage()
returns CTBotMessageQuery
value and the TBMessage
data structure is filled with the callback data.
When query button is pressed, is mandatory to notify the Telegram Server the end of the query process by calling the endQuery()
method.
Here an example:
#include "CTBot.h"
#define CALLBACK_QUERY_DATA "QueryData" // callback data sent when the button is pressed
CTBot myBot;
CTBotInlineKeyboard myKbd; // custom inline keyboard object helper
void setup() {
Serial.begin(115200); // initialize the serial
myBot.wifiConnect("mySSID", "myPassword"); // connect to the WiFi Network
myBot.setTelegramToken("myTelegramBotToken"); // set the telegram bot token
// inline keyboard - only a button called "My button"
myKbd.addButton("My button", CALLBACK_QUERY_DATA, CTBotKeyboardButtonQuery);
}
void loop() {
TBMessage msg; // a variable to store telegram message data
// if there is an incoming message...
if (myBot.getNewMessage(msg)) {
// ...and if it is a callback query message
if (msg.messageType == CTBotMessageQuery) {
// received a callback query message, check if it is the "My button" callback
if (msg.callbackQueryData.equals(CALLBACK_QUERY_DATA)) {
// pushed "My button" button --> do related things...
// close the callback query
myBot.endQuery(msg.callbackQueryID, "My button pressed");
}
} else {
// the received message is a text message --> reply with the inline keyboard
myBot.sendMessage(msg.sender.id, "Inline Keyboard", myKbd);
}
}
delay(500); // wait 500 milliseconds
}
See the inlineKeyboard example for further details.
There are several usefully data structures used to store data typically sent by the Telegram Server.
TBUser
data type is used to store user data like Telegram userID. The data structure contains:
uint32_t id;
bool isBot;
String firstName;
String lastName;
String username;
String languageCode;
where:
id
is the unique Telegram user IDisBot
tells if the user IDid
refers to a bot (true
value) or not (false
value)firstName
contains the first name (if provided) of the user IDid
lastName
contains the last name (if provided) of the user IDid
username
contains the username of the user IDid
languageCode
contains the country code used by the user IDid
Typically, you will use predominantly the id
field.
TBLocation
data type is used to store the longitude and the latitude. The data structure contains:
float longitude;
float latitude;
where:
longitude
contains the value of the longitudelatitude
contains the value of the latitude
For localization messages, see TBMessage
TBGroup
data type is used to store the group chat data. The data structure contains:
int64_t id;
String title;
where:
id
contains the ID of the group chattitle
contains the title of the group chat
TBContact
data type is used to store the contact data. The data structure contains:
String phoneNumber;
String firstName;
String lastName;
int32_t id;
String vCard;
where:
phoneNumber
contains the phone number of the contactfirstName
contains the first name of the contactlastName
contains the last name of the contactid
contains the ID of the contactvCard
contains the vCard of the contact
TBMessage
data type is used to store new messages. The data structure contains:
uint32_t messageID;
TBUser sender;
TBGroup group;
uint32_t date;
String text;
String chatInstance;
String callbackQueryData;
String callbackQueryID;
TBLocation location;
TBcontact contact;
CTBotMessageType messageType;
where:
messageID
contains the unique message identifier associated to the received messagesender
contains the sender data in a TBUser structuregroup
contains the group chat data in a TBGroup structuredate
contains the date when the message was sent, in Unix timetext
contains the received message (if a text message is received - see CTBot::getNewMessage())chatInstance
contains the unique ID corresponding to the chat to which the message with the callback button was sentcallbackQueryData
contains the data associated with the callback buttoncallbackQueryID
contains the unique ID for the querylocation
contains the location's longitude and latitude (if a location message is received - see CTBot::getNewMessage())contact
contains the contact information a TBContact structuremessageType
contains the message type. See CTBotMessageType
There are several usefully enumerators used to define method parameters or method return value.
Enumerator used to define the possible message types received by getNewMessage() method. Used also by TBMessage.
enum CTBotMessageType {
CTBotMessageNoData = 0,
CTBotMessageText = 1,
CTBotMessageQuery = 2,
CTBotMessageLocation = 3,
CTBotMessageContact = 4
};
where:
CTBotMessageNoData
: error - the TBMessage structure contains no valid dataCTBotMessageText
: the TBMessage structure contains a text messageCTBotMessageQuery
: the TBMessage structure contains a calback query message (see Inline Keyboards)CTBotMessageLocation
: the TBMessage structure contains a localization messageCTBotMessageContact
: the TBMessage structure contains a contact message
Enumerator used to define the possible button types. Button types are used when creating an inline keyboard with addButton() method.
enum CTBotInlineKeyboardButtonType {
CTBotKeyboardButtonURL = 1,
CTBotKeyboardButtonQuery = 2
};
where:
CTBotKeyboardButtonURL
: define a URL button. When pressed, Telegram client will ask if open the URL in a browserCTBotKeyboardButtonQuery
: define a calback query button. When pressed, a callback query message is sent to the bot
Here you can find the basic member function. First you have to instantiate a CTBot object, like CTbot myBot
, then call the desired member function as myBot.myDesiredFunction()
bool CTBot::wifiConnect(String ssid, String password)
Use this member function to connect the ESP8266 board to a WiFi Network. By default, it's a locking operation (the execution is locked until the connection is established), see setMaxConnectionRetries() for further details.
Parameters:
ssid
: the WiFi Network SSIDpassword
: (optional) the password of the WiFi Network
Returns: true
if a connection to the specified WiFi Network is established.
Examples:
wifiConnect("mySSID")
: connect to a WiFi network named mySSIDwifiConnect("mySSID", "myPassword")
: connect to a WiFi network named mySSID with password myPassword
void CTBot::setTelegramToken(String token)
Set the Telegram Bot token. If you need infos about Telegram Bot and how to obtain a token, take a look here.
Parameters:
token
: the token that identify the Telegram Bot
Returns: none.
Example:
setTelegramToken("myTelegramBotToken")
bool CTBot::setIP(String ip, String gateway, String subnetMask, String dns1, String dns2)
By default, once connected the ESP8266 get the IP from the DHCP Server. With this function is possible to set the IP of the ESP8266 as a static IP.
Parameters:
ip
: the fixed IP addressgateway
: the gateway addresssubnetMask
: the subnet maskdns1
: (optional) the first DNSdns2
: (optional) the second DNS
Returns: true
if no error occurred.
Examples:
setIP("192.168.0.130", "192.168.0.254", "255.255.255.0")
: set the static IP 192.168.0.130, the gateway 192.168.0.254 and the subnet mask 255.255.255.0setIP("192.168.0.130", "192.168.0.254", "255.255.255.0", "8.8.8.8")
: set the static IP 192.168.0.130, the gateway 192.168.0.254, the subnet mask 255.255.255.0 and the primary DNS 8.8.8.8setIP("192.168.0.130", "192.168.0.254", "255.255.255.0", "8.8.8.8", "8.8.4.4")
: set the static IP 192.168.0.130, the gateway 192.168.0.254, the subnet mask 255.255.255.0, the primary DNS 8.8.8.8 and the secondary DNS 8.8.4.4
bool CTBot::testConnection(void)
Check the connection between ESP8266 board and the Telegram server.
Parameters: none
Returns: true
if the ESP8266 is able to send/receive data to/from the Telegram server.
Example:
#include "CTBot.h"
CTBot myBot;
void setup() {
Serial.begin(115200); // initialize the serial
myBot.wifiConnect("mySSID", "myPassword"); // connect to the WiFi Network
myBot.setTelegramToken("myTelegramBotToken"); // set the telegram bot token
if(myBot.testConnection())
Serial.println("Connection OK");
else
Serial.println("Connectionk NOK");
}
void loop() {
}
bool CTBot::getNewMessage(TBMessage &message)
CTBotMessageType CTBot::getNewMessage(TBMessage &message)
Get the first unread message from the message queue. Fetch text message and callback query message (for callback query messages, see Inline Keyboards). This is a destructive operation: once read, the message will be marked as read so a new getNewMessage
will fetch the next message (if any).
Parameters:
message
: aTBMessage
data structure that will contains the message data retrieved
Returns: true
if there is a new message and fill the message
parameter with the received message data.
Returns:
CTBotMessageNoData
if an error occurredCTBotMessageText
if the message received is a text messageCTBotMessageQuery
if the message received is a callback query message (see Handling callback messages)CTBotMessageLocation
if the message received is a location messageCTBotMessageContact
if the message received is a contact message
Compatibility with previous versions: you can still use the false
statement to check if the getNewMessage
method got errors as the following example do.
IMPORTANT: before using the data inside the message
parameter, always check the return value: a false
CTBotMessageNoData
return value means that there are no valid data stored inside the message
parameter. See the following example.
Example:
#include "CTBot.h"
CTBot myBot;
void setup() {
Serial.begin(115200); // initialize the serial
myBot.wifiConnect("mySSID", "myPassword"); // connect to the WiFi Network
myBot.setTelegramToken("myTelegramBotToken"); // set the telegram bot token
}
void loop() {
TBMessage msg; // a variable to store telegram message data
// check if there is a new incoming message
if (myBot.getNewMessage(msg)) {
// there is a valid message in msg
Serial.print("Received message from: ");
Serial.println(msg.sender.username);
if (msg.messageType == CTBotMessageText) {
// a text message is received
Serial.print("Text: ");
Serial.println(msg.text);
}
else if (msg.messageType == CTBotMessageLocation) {
// a position/location message is received
Serial.println("Position");
Serial.print(" - Latitude : ");
Serial.println(msg.location.latitude, 5);
Serial.print(" - Longitude: ");
Serial.println(msg.location.longitude, 5);
}
else
Serial.println("Invalid message received.");
}
else {
// no valid message in msg
Serial.println("No new message");
}
delay(500); // wait 500 milliseconds
}
bool CTBot::sendMessage(uint32_t id, String message, String keyboard)
bool CTBot::sendMessage(uint32_t id, String message, CTBotInlineKeyboard keyboard)
bool CTBot::sendMessage(int64_t id, String message, CTBotReplyKeyboard &keyboard)
Send a message to the specified Telegram user ID.
If keyboard
parameter is specified, send the message and display the custom keyboard (inline or reply).
- Inline keyboard are defined by a JSON structure (see the Telegram API documentation InlineKeyboardMarkup)
You can also use the helper class CTBotInlineKeyboard for creating inline keyboards. - Reply keyboard are define by a JSON structure (see Telegram API documentation ReplyKeyboardMarkup)
You can also use the helper class CTBotReplyKeyboard for creating inline keyboards.
Parameters:
id
: the recipient Telegram user IDmessage
: the message to sendkeyboard
: (optional) the inline/reply keyboard
Returns: true
if no error occurred.
Example:
#include "CTBot.h"
CTBot myBot;
void setup() {
Serial.begin(115200); // initialize the serial
myBot.wifiConnect("mySSID", "myPassword"); // connect to the WiFi Network
myBot.setTelegramToken("myTelegramBotToken"); // set the telegram bot token
}
void loop() {
TBMessage msg; // a variable to store telegram message data
// if there is an incoming message...
if (myBot.getNewMessage(msg))
// ...forward it to the sender
myBot.sendMessage(msg.sender.id, msg.text);
delay(500); // wait 500 milliseconds
}
Examples using inline keyboard can be found here:
bool endQuery(String queryID, String message = "", bool alertMode = false)
Terminate a query started by pressing an inlineKeyboard button. See Handling callback messages for further details.
Parameters:
queryID
: the unique query ID (retrieved with getNewMessage method)message
: (optional) a message to displayalertMode
: (optional) the way how to display the message:false
display a popup messagetrue
display an alert windowed message with an ok button
Returns: true
if no error occurred.
Example:
#include "CTBot.h"
#define CALLBACK_QUERY_DATA "QueryData" // callback data sent when the button is pressed
CTBot myBot;
CTBotInlineKeyboard myKbd; // custom inline keyboard object helper
void setup() {
Serial.begin(115200); // initialize the serial
myBot.wifiConnect("mySSID", "myPassword"); // connect to the WiFi Network
myBot.setTelegramToken("myTelegramBotToken"); // set the telegram bot token
// inline keyboard - only a button called "My button"
myKbd.addButton("My button", CALLBACK_QUERY_DATA, CTBotKeyboardButtonQuery);
}
void loop() {
TBMessage msg; // a variable to store telegram message data
// if there is an incoming message...
if (myBot.getNewMessage(msg)) {
// ...and if it is a callback query message
if (msg.messageType == CTBotMessageQuery) {
// received a callback query message, check if it is the "My button" callback
if (msg.callbackQueryData.equals(CALLBACK_QUERY_DATA)) {
// pushed "My button" button --> do related things...
// close the callback query
myBot.endQuery(msg.callbackQueryID, "My button pressed");
}
} else {
// the received message is a text message --> reply with the inline keyboard
myBot.sendMessage(msg.sender.id, "Inline Keyboard", myKbd);
}
}
delay(500); // wait 500 milliseconds
}
bool removeReplyKeyboard(int64_t id, String message, bool selective = false)
Remove an active replyKeyboard for a specified user by sending a message.
Parameters:
id
: the Telegram user IDmessage
: the message to be show to the selected user IDselective
: (optional) enable the selective mode (hide the keyboard for specific users only). Useful for hiding the keyboard for users that are @mentioned in the text of the Message object or if the bot's message is a reply (has reply_to_message_id), sender of the original message
Returns: true
if no error occurred.
Example:
VALUTARE ESEMPIO
bool CTBotInlineKeyboard::addButton(String text, String command, CTBotInlineKeyboardButtonType buttonType)
Add a button to the current keyboard row of an CTBotInlineKeyboard object. For a description of button types, see Inline Keyboards.
Parameters:
text
: the botton text (label) displayed on the inline keyboardcommand
: depending on the button type,- on URL buttons, contain the URL
- on a query button, contain the query data
buttonType
: set the behavior of the button. It can be:CTBotKeyboardButtonURL
- the added button will be a URL buttonCTBotKeyboardButtonQuery
- the added button will be a query button Returns:true
if no error occurred.
Example
CTBotInlineKeyboard kbd; // create an inline keyboard object
// add an URL button to the inline keyboard
kbd.addButton("My URL Button", "URL", CTBotKeyboardButtonURL);
// add an URL button to the inline keyboard
kbd.addButton("My Query Button", "queryData", CTBotKeyboardButtonQuery);
bool CTBotInlineKeyboard::addRow(void)
Add a new empty row of buttons to the inline keyboard: all the new keyboard buttons will be added to this new row.
Parameters: none
Returns: true
if no error occurred.
Example
CTBotInlineKeyboard kbd; // create an inline keyboard object
// add an URL button to the inline keyboard
kbd.addButton("My URL Button", "URL", CTBotKeyboardButtonURL);
// add an URL button to the inline keyboard
kbd.addButton("My Query Button", "queryData", CTBotKeyboardButtonQuery);
kbd.addRow(); // new row: all the new buttons will be added here
// this button will be added to the new row.
kbd.addButton("My Button", "anotherQueryData", CTBotKeyboardButtonQuery);
void CTBotInlineKeyboard::flushData(void)
Remove all buttons/rows from an inline keyboard and initialize it to a new inline keyboard.
Parameters: none
Returns: none
Example
CTBotInlineKeyboard kbd; // create an inline keyboard object
// add an URL button to the inline keyboard
kbd.addButton("My URL Button", "URL", CTBotKeyboardButtonURL);
// add an URL button to the inline keyboard
kbd.addButton("My Query Button", "queryData", CTBotKeyboardButtonQuery);
kbd.addRow(); // new row: all the new buttons will be added here
// this button will be added to the new row.
kbd.addButton("My Button", "anotherQueryData", CTBotKeyboardButtonQuery);
...
kbd.flushData(); // now the keyboard is empty
String CTBotInlineKeyboard::getJSON(void)
Create a string that containsthe inline keyboard formatted in a JSON structure. Useful sending the inline keyboard with sendMessage().
Parameters: none
Returns: the JSON of the inline keyboard
Example
CTBotInlineKeyboard kbd; // create an inline keyboard object
// add an URL button to the inline keyboard
kbd.addButton("My URL Button", "URL", CTBotKeyboardButtonURL);
// add an URL button to the inline keyboard
kbd.addButton("My Query Button", "queryData", CTBotKeyboardButtonQuery);
String kbdJSON = kbd.getJSON();
When instantiated, a CTBot object is configured as follow:
- If the
wifiConnect()
method is executed, it wait until a connection with the specified WiFi network is established (locking operation). See setMaxConnectionRetries(). - Use the Telegram server static IP (149.154.167.198). See useDNS().
- The incoming messages are not converted to UTF8. See enableUTF8Encoding().
- The status pin is disabled. See setStatusPin().
With the following methods, is possible to change the behavior of the CTBot instantiated object.
void CTBot::setMaxConnectionRetries(uint8_t retries)
Set how many times the wifiConnect()
method have to try to connect to the specified SSID. After each try, the wifiConnect()
wait 500 milliseconds.
A value of zero mean infinite retries.
Default value is zero (infinite retries).
Parameters:
retries
: how many times wifiConnect have to try to connect. Zero means infinites tries (locking).
Returns: none.
Example 1: finite retries
#include "CTBot.h"
CTBot myBot;
void setup() {
bool status;
Serial.begin(115200); // initialize the serial
myBot.setMaxConnectionRetries(15); // try 15 times to connect to the specified SSID (mySSID)
status = myBot.wifiConnect("mySSID", "myPassword"); // try connect (15 times) to the WiFi Network
if (status == true)
// connection successful!
Serial.println("Connection established!");
else
// after 15 tries, the ESP8266 can't connect to the specified SSID
Serial.println("Unable to connect to the WiFi network");
}
void loop() {
}
Example 2: infinite retries
#include "CTBot.h"
CTBot myBot;
void setup() {
Serial.begin(115200); // initialize the serial
myBot.setMaxConnectionRetries(0); // try infinite times (default value)
// to connect to the specified SSID in wifiConnect
myBot.wifiConnect("mySSID", "myPassword"); // try connect (infinite times) to the WiFi Network
// the rest of the code is executed ONLY if there is an established connection (wifiConnect() is locking).
}
void loop() {
}
void CTBot::useDNS(bool value)
Define which kind of address (symbolic address or fixed IP) will be used to establish connections with the Telegram server.
Default value is false
(use fixed IP)
Is better to use fixed IP when no DNS server are provided.
Parameters:
value
: settrue
if you want to use the URL style address "api.telegram.org" or setfalse
if you want to use the fixed IP address "149.154.167.198".
Returns: none.
Examples:
useDNS(true)
: for every connection with the Telegram server, will be used the URL style address "api.telegram.org"useDNS(false)
: for every connection with the Telegram server, will be used the fixed IP address "149.154.167.198"
void CTBot::enableUTF8Encoding(bool value)
Tipically, Telegram server encodes messages with an UNICODE like format. This mean for example that a '€' character is sent by Telegram server encoded in this form \u20AC (UNICODE). For some weird reasons, the backslash character disappears and the message you get is u20AC thus is impossible to correctly decode an incoming message.
Encoding the received message with UTF8 encoding format will solve the problem.
Encoding messages in UTF8 format will consume a bit of CPU time.
Default value is false
(no UTF8 conversion).
Parameters:
value
: settrue
to enable the UTF8 encoding for all incoming messages; setfalse
to disable this feature.
Returns: none.
Examples:
enableUTF8Encoding(true)
: every incoming message will be encoded in UTF8enableUTF8Encoding(false)
: every incoming message is encoded as Telegram server do
void CTBot::setStatusPin(int8_t pin)
A status pin is used to send blinking notification by connecting to the specified pin to a LED.
Actually there are two notification:
- During the connection process to a WiFi network, the status pin will blink regularly.
- Every time a command is sent to the Telegram server, the status pin will pulse.
Default value is CTBOT_DISABLE_STATUS_PIN
(status pin disable).
Parameters:
pin
: the Arduino like pin to use as status pin. to disable this feature, set it toCTBOT_DISABLE_STATUS_PIN
Returns: none.
Example:
setStatusPin(2)
: enable the status pin feature using the pin 2 (GPIO 4 - onboard LED of the ESP8266 chip)
void CTBot::setFingerprint(const uint8_t *newFingerprint)
Set the new Telegram API server fingerprint overwriting the default one.
The fingerprint can be obtained by this service provided by Gibson Research Corporation. To obtain the new fingerprint, just query for api.telegram.org
Default value is BB:DC:45:2A:07:E3:4A:71:33:40:32:DA:BE:81:F7:72:6F:4A:2B:6B
.
Parameters:
newFingerprint
: the 20 bytes array that contains the new fingerprint.
Returns: none.
Example:
void setup() {
...
uint8_t telegramFingerprint [20] = { 0xBB, 0xDC, 0x45, 0x2A, 0x07, 0xE3, 0x4A, 0x71, 0x33, 0x40, 0x32, 0xDA, 0xBE, 0x81, 0xF7, 0x72, 0x6F, 0x4A, 0x2B, 0x6B };
myBot.setFingerprint(telegramFingerprint);
...
}
void loop(){
...
}