Skip to content

Instantly share code, notes, and snippets.

@cotestatnt
Last active July 26, 2021 21:58
Show Gist options
  • Save cotestatnt/eff3d2acbbf6c57410c6ebfc1764eef4 to your computer and use it in GitHub Desktop.
Save cotestatnt/eff3d2acbbf6c57410c6ebfc1764eef4 to your computer and use it in GitHub Desktop.
ESP8266 AsyncTelegram2 test
#include <AsyncTelegram2.h>
// Timezone definition
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
#include <time.h>
#include <ESP8266WiFi.h>
BearSSL::WiFiClientSecure client;
BearSSL::Session session;
BearSSL::X509List certificate(telegram_cert);
AsyncTelegram2 myBot(client);
const char* ssid = "xxxxxxxxx"; // SSID WiFi network
const char* pass = "xxxxxxxxx"; // Password WiFi network
const char* token = "xxxxxxxxx"; // Telegram token
// Check the userid with the help of bot @JsonDumpBot or @getidsbot (work also with groups)
// https://t.me/JsonDumpBot or https://t.me/getidsbot
int64_t userid = 1234567890;
ReplyKeyboard myReplyKbd; // reply keyboard object helper
InlineKeyboard myInlineKbd; // inline keyboard object helper
bool isKeyboardActive; // store if the reply keyboard is shown
#define LIGHT_ON_CALLBACK "lightON" // callback data sent when "LIGHT ON" button is pressed
#define LIGHT_OFF_CALLBACK "lightOFF" // callback data sent when "LIGHT OFF" button is pressed
const uint8_t LED = 4;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED, OUTPUT);
// initialize the Serial
Serial.begin(115200);
WiFi.setAutoConnect(true);
WiFi.mode(WIFI_STA);
// connects to the access point
WiFi.begin(ssid, pass);
delay(500);
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
// Sync time with NTP, to check properly Telegram certificate
configTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
//Set certficate, session and some other base client properies
client.setSession(&session);
client.setTrustAnchors(&certificate);
client.setBufferSizes(1024, 1024);
// Set the Telegram bot properties
myBot.setUpdateTime(1000);
myBot.setTelegramToken(token);
// Check if all things are ok
Serial.print("\nTest Telegram connection... ");
myBot.begin() ? Serial.println("OK") : Serial.println("NOK");
Serial.print("Bot name: @");
Serial.println(myBot.getBotName());
// Add reply keyboard
isKeyboardActive = false;
// add a button that send a message with "Simple button" text
myReplyKbd.addButton("Button1");
myReplyKbd.addButton("Button2");
myReplyKbd.addButton("Button3");
// add a new empty button row
myReplyKbd.addRow();
// add another button that send the user position (location)
myReplyKbd.addButton("Send Location", KeyboardButtonLocation);
// add another button that send the user contact
myReplyKbd.addButton("Send contact", KeyboardButtonContact);
// add a new empty button row
myReplyKbd.addRow();
// add a button that send a message with "Hide replyKeyboard" text
// (it will be used to hide the reply keyboard)
myReplyKbd.addButton("/hide_keyboard");
// resize the keyboard to fit only the needed space
myReplyKbd.enableResize();
// Add sample inline keyboard
myInlineKbd.addButton("ON", LIGHT_ON_CALLBACK, KeyboardButtonQuery);
myInlineKbd.addButton("OFF", LIGHT_OFF_CALLBACK, KeyboardButtonQuery);
myInlineKbd.addRow();
myInlineKbd.addButton("GitHub", "https://github.com/cotestatnt/AsyncTelegram/", KeyboardButtonURL);
const char* botName = myBot.getBotName();
Serial.printf("Nome del bot: @%s", botName);
char welcome_msg[128];
snprintf(welcome_msg, 128, PSTR("BOT @%s online\n/help all commands avalaible."), botName);
myBot.sendTo(userid, welcome_msg);
}
void loop() {
// In the meantime LED_BUILTIN will blink with a fixed frequency
// to evaluate async and non-blocking working of library
static uint32_t ledTime = millis();
if (millis() - ledTime > 200) {
ledTime = millis();
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
static uint32_t aliveTime = millis();
if (millis() - aliveTime > 300000) {
aliveTime = millis();
time_t epoch = time(nullptr);
tm now = *localtime(&epoch);
char buffer [50];
strftime (buffer, 50, "Alive: %c", &now);
myBot.sendTo(userid, buffer);
}
// local variable to store telegram message data
TBMessage msg;
// if there is an incoming message...
if (myBot.getNewMessage(msg)) {
// check what kind of message I received
MessageType msgType = msg.messageType;
String msgText = msg.text;
switch (msgType) {
case MessageText :
// received a text message
Serial.print("\nText message received: ");
Serial.println(msgText);
// check if is show keyboard command
if (msgText.equalsIgnoreCase("/reply_keyboard")) {
// the user is asking to show the reply keyboard --> show it
myBot.sendMessage(msg, "This is reply keyboard:", myReplyKbd);
isKeyboardActive = true;
}
else if (msgText.equalsIgnoreCase("/inline_keyboard")) {
myBot.sendMessage(msg, "This is inline keyboard:", myInlineKbd);
}
// check if the reply keyboard is active
else if (isKeyboardActive) {
// is active -> manage the text messages sent by pressing the reply keyboard buttons
if (msgText.equalsIgnoreCase("/hide_keyboard")) {
// sent the "hide keyboard" message --> hide the reply keyboard
myBot.removeReplyKeyboard(msg, "Reply keyboard removed");
isKeyboardActive = false;
} else {
// print every others messages received
myBot.sendMessage(msg, msg.text);
}
}
// the user write anything else and the reply keyboard is not active --> show a hint message
else {
myBot.sendMessage(msg, "Try /reply_keyboard or /inline_keyboard");
}
break;
case MessageQuery:
// received a callback query message
msgText = msg.callbackQueryData;
Serial.print("\nCallback query message received: ");
Serial.println(msgText);
if (msgText.equalsIgnoreCase(LIGHT_ON_CALLBACK)) {
// pushed "LIGHT ON" button...
Serial.println("\nSet light ON");
digitalWrite(LED, HIGH);
// terminate the callback with an alert message
myBot.endQuery(msg, "Light on", true);
}
else if (msgText.equalsIgnoreCase(LIGHT_OFF_CALLBACK)) {
// pushed "LIGHT OFF" button...
Serial.println("\nSet light OFF");
digitalWrite(LED, LOW);
// terminate the callback with a popup message
myBot.endQuery(msg, "Light off");
}
break;
case MessageLocation:
// received a location message --> send a message with the location coordinates
char bufL[50];
snprintf(bufL, sizeof(bufL), "Longitude: %f\nLatitude: %f\n", msg.location.longitude, msg.location.latitude) ;
myBot.sendMessage(msg, bufL);
Serial.println(bufL);
break;
case MessageContact:
char bufC[50];
snprintf(bufC, sizeof(bufC), "Contact information received: %s - %s\n", msg.contact.firstName, msg.contact.phoneNumber ) ;
// received a contact message --> send a message with the contact information
myBot.sendMessage(msg, bufC);
Serial.println(bufC);
break;
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment