Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: keyboards.ino
- Created: 20/06/2020
- Author: Tolentino Cotesta <[email protected]>
- Description: a more complex example that do:
- 1) if a "/inline_keyboard" text message is received, show the
- inline custom keyboard, if a "/reply_keyboard" text message is received, show
- the reply custom keyboard, otherwise reply the sender with "Try
- /reply_keyboard or /inline_keyboard" message 2) if "LIGHT ON" inline keyboard
- button is pressed turn on the LED and show a message 3) if "LIGHT OFF" inline
- keyboard button is pressed, turn off the LED and show a message 4) if "GitHub"
- inline keyboard button is pressed, open a browser window with URL
- "https://github.com/cotestatnt/AsyncTelegram"
- */
- #include <AsyncTelegram.h>
- #include <rom/rtc.h>
- const char * verbose_print_reset_reason(RESET_REASON reason) {
- switch (reason) {
- case 1:
- return ("Vbat power on reset");
- break;
- case 3:
- return ("Software reset digital core");
- break;
- case 4:
- return ("Legacy watch dog reset digital core");
- break;
- case 5:
- return ("Deep Sleep reset digital core");
- break;
- case 6:
- return ("Reset by SLC module, reset digital core");
- break;
- case 7:
- return ("Timer Group0 Watch dog reset digital core");
- break;
- case 8:
- return ("Timer Group1 Watch dog reset digital core");
- break;
- case 9:
- return ("RTC Watch dog Reset digital core");
- break;
- case 10:
- return ("Instrusion tested to reset CPU");
- break;
- case 11:
- return ("Time Group reset CPU");
- break;
- case 12:
- return ("Software reset CPU");
- break;
- case 13:
- return ("RTC Watch dog Reset CPU");
- break;
- case 14:
- return ("for APP CPU, reseted by PRO CPU");
- break;
- case 15:
- return ("Reset when the vdd voltage is not stable");
- break;
- case 16:
- return ("RTC Watch dog reset digital core and rtc module");
- break;
- default:
- return ("NO_MEAN");
- }
- }
- AsyncTelegram myBot;
- ReplyKeyboard myReplyKbd; // reply keyboard object helper
- InlineKeyboard myInlineKbd; // inline keyboard object helper
- bool isKeyboardActive; // store if the reply keyboard is shown
- const char *ssid = "xxxxxxx"; // SSID WiFi network
- const char *pass = "xxxxxxx"; // Password WiFi networ
- const char* token = "xxxxxxxxx:AAFuMQ1E2smMxxxxxxxxokzwxgpvNBzJwg"; //
- // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
- #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
- #ifndef LED_BUILTIN
- #define LED_BUILTIN 2
- #endif
- const uint8_t LED = 4;
- #if defined(ESP32)
- // WiFi event handler
- void WiFiEvent(WiFiEvent_t event) {
- switch (event) {
- case SYSTEM_EVENT_STA_GOT_IP:
- Serial.print("\nWiFi connected! IP address: ");
- Serial.println(WiFi.localIP());
- break;
- case SYSTEM_EVENT_STA_DISCONNECTED:
- Serial.println("\nWiFi lost connection");
- WiFi.setAutoReconnect(true);
- myBot.reset();
- break;
- default:
- break;
- }
- }
- #endif
- void printHeapStats() {
- time_t now = time(nullptr);
- struct tm tInfo = *localtime(&now);
- static uint32_t infoTime;
- if (millis() - infoTime > 10000) {
- infoTime = millis();
- #ifdef ESP32
- Serial.printf("\n%02d:%02d:%02d - Total free: %6d - Max block: %6d\n",
- tInfo.tm_hour, tInfo.tm_min, tInfo.tm_sec,
- heap_caps_get_free_size(0),
- heap_caps_get_largest_free_block(0));
- #elif defined(ESP8266)
- uint32_t free;
- uint16_t max;
- ESP.getHeapStats(&free, &max, nullptr);
- Serial.printf("\nTotal free: %5d - Max block: %5d\n", free, max);
- #endif
- }
- }
- void setup() {
- pinMode(LED_BUILTIN, OUTPUT);
- pinMode(LED, OUTPUT);
- // initialize the Serial
- Serial.begin(115200);
- WiFi.setAutoConnect(true);
- WiFi.mode(WIFI_STA);
- #if defined(ESP32)
- Serial.printf("setup() running on core %d\n", xPortGetCoreID());
- WiFi.onEvent(WiFiEvent);
- #endif
- Serial.printf("Free heap: %d\n", ESP.getFreeHeap());
- Serial.print("\n\nStart connection to WiFi...");
- delay(100);
- // connects to access point
- WiFi.begin(ssid, pass);
- delay(500);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print('.');
- delay(500);
- }
- Serial.println("CPUs reset reason:");
- const char *resetCPU0 = verbose_print_reset_reason(rtc_get_reset_reason(0));
- const char *resetCPU1 = verbose_print_reset_reason(rtc_get_reset_reason(1));
- Serial.println(resetCPU0);
- Serial.println(resetCPU1);
- Serial.println();
- // To ensure certificate validation, WiFiClientSecure needs time upadated
- // myBot.setInsecure(false);
- myBot.setClock("CET-1CEST,M3.5.0,M10.5.0/3");
- // Set the Telegram bot properies
- 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");
- // 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.userName.c_str();
- Serial.printf("Nome del bot: @%s\n%s\n%s", botName, resetCPU0, resetCPU1);
- time_t now = time(nullptr);
- struct tm tInfo = *localtime(&now);
- char welcome_msg[128];
- snprintf(welcome_msg, 128,
- PSTR("%02d:%02d:%02d - <b>@%s</b> online!\n%s\n%s"), tInfo.tm_hour,
- tInfo.tm_min, tInfo.tm_sec, botName, resetCPU0, resetCPU1);
- TBMessage msg;
- msg.sender.id =
- 436865110; // You can discover your own chat id, with "Json Dump Bot"
- msg.isHTMLenabled = true;
- myBot.sendMessage(msg, welcome_msg);
- }
- void loop() {
- printHeapStats();
- // 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));
- }
- // a 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
- String tgReply;
- MessageType msgType = msg.messageType;
- switch (msgType) {
- case MessageText:
- // received a text message
- tgReply = msg.text;
- Serial.print("\nText message received: ");
- Serial.println(tgReply);
- // check if is show keyboard command
- if (tgReply.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 (tgReply.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 (tgReply.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
- tgReply = msg.callbackQueryData;
- Serial.print("\nCallback query message received: ");
- Serial.println(tgReply);
- if (tgReply.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 (tgReply.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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement