|
| 1 | +/******************************************************************* |
| 2 | + An example of how to use a custom reply keyboard markup. |
| 3 | +
|
| 4 | +
|
| 5 | + written by Vadim Sinitski (modified by Brian Lough) |
| 6 | + *******************************************************************/ |
| 7 | +#include <WiFi.h> |
| 8 | +#include <WiFiClientSecure.h> |
| 9 | +#include <UniversalTelegramBot.h> |
| 10 | + |
| 11 | +// Initialize Wifi connection to the router |
| 12 | +char ssid[] = "XXXXXX"; // your network SSID (name) |
| 13 | +char password[] = "YYYYYY"; // your network key |
| 14 | + |
| 15 | +// Initialize Telegram BOT |
| 16 | +#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather) |
| 17 | + |
| 18 | +WiFiClientSecure client; |
| 19 | +UniversalTelegramBot bot(BOTtoken, client); |
| 20 | + |
| 21 | +int Bot_mtbs = 1000; //mean time between scan messages |
| 22 | +long Bot_lasttime; //last time messages' scan has been done |
| 23 | + |
| 24 | +void handleNewMessages(int numNewMessages) { |
| 25 | + |
| 26 | + for (int i = 0; i < numNewMessages; i++) { |
| 27 | + |
| 28 | + // Inline buttons with callbacks when pressed will raise a callback_query message |
| 29 | + if (bot.messages[i].type == "callback_query") { |
| 30 | + Serial.print("Call back button pressed by: "); |
| 31 | + Serial.println(bot.messages[i].from_id); |
| 32 | + Serial.print("Data on the button: "); |
| 33 | + Serial.println(bot.messages[i].text); |
| 34 | + bot.sendMessage(bot.messages[i].from_id, bot.messages[i].text, ""); |
| 35 | + } else { |
| 36 | + String chat_id = String(bot.messages[i].chat_id); |
| 37 | + String text = bot.messages[i].text; |
| 38 | + |
| 39 | + String from_name = bot.messages[i].from_name; |
| 40 | + if (from_name == "") from_name = "Guest"; |
| 41 | + |
| 42 | + if (text == "/options") { |
| 43 | + String keyboardJson = "[[{ \"text\" : \"Go to Google\", \"url\" : \"https://www.google.com\" }],[{ \"text\" : \"Send\", \"callback_data\" : \"This was sent by inline\" }]]"; |
| 44 | + bot.sendMessageWithInlineKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson); |
| 45 | + } |
| 46 | + |
| 47 | + if (text == "/start") { |
| 48 | + String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n"; |
| 49 | + welcome += "This is Inline Keyboard Markup example.\n\n"; |
| 50 | + welcome += "/options : returns the inline keyboard\n"; |
| 51 | + |
| 52 | + bot.sendMessage(chat_id, welcome, "Markdown"); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void setup() { |
| 59 | + Serial.begin(115200); |
| 60 | + |
| 61 | + // Attempt to connect to Wifi network: |
| 62 | + Serial.print("Connecting Wifi: "); |
| 63 | + Serial.println(ssid); |
| 64 | + |
| 65 | + // Set WiFi to station mode and disconnect from an AP if it was Previously |
| 66 | + // connected |
| 67 | + WiFi.mode(WIFI_STA); |
| 68 | + WiFi.begin(ssid, password); |
| 69 | + |
| 70 | + while (WiFi.status() != WL_CONNECTED) { |
| 71 | + Serial.print("."); |
| 72 | + delay(500); |
| 73 | + } |
| 74 | + |
| 75 | + Serial.println(""); |
| 76 | + Serial.println("WiFi connected"); |
| 77 | + Serial.print("IP address: "); |
| 78 | + Serial.println(WiFi.localIP()); |
| 79 | +} |
| 80 | + |
| 81 | +void loop() { |
| 82 | + if (millis() > Bot_lasttime + Bot_mtbs) { |
| 83 | + int numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 84 | + |
| 85 | + while (numNewMessages) { |
| 86 | + Serial.println("got response"); |
| 87 | + handleNewMessages(numNewMessages); |
| 88 | + numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 89 | + } |
| 90 | + |
| 91 | + Bot_lasttime = millis(); |
| 92 | + } |
| 93 | +} |
0 commit comments