|
| 1 | +/******************************************************************* |
| 2 | +* An example of bot that show bot action message. * |
| 3 | +* * |
| 4 | +* * |
| 5 | +* * |
| 6 | +* written by Vadim Sinitski * |
| 7 | + *******************************************************************/ |
| 8 | +#include <ESP8266WiFi.h> |
| 9 | +#include <WiFiClientSecure.h> |
| 10 | +#include <UniversalTelegramBot.h> |
| 11 | + |
| 12 | +// Initialize Wifi connection to the router |
| 13 | +char ssid[] = "XXXXXX"; // your network SSID (name) |
| 14 | +char password[] = "YYYYYY"; // your network key |
| 15 | + |
| 16 | +// Initialize Telegram BOT |
| 17 | +#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather) |
| 18 | + |
| 19 | +WiFiClientSecure client; |
| 20 | +UniversalTelegramBot bot(BOTtoken, client); |
| 21 | + |
| 22 | +int Bot_mtbs = 1000; //mean time between scan messages |
| 23 | +long Bot_lasttime; //last time messages' scan has been done |
| 24 | +bool Start = false; |
| 25 | + |
| 26 | +void handleNewMessages(int numNewMessages) { |
| 27 | + Serial.println("handleNewMessages"); |
| 28 | + Serial.println(String(numNewMessages)); |
| 29 | + |
| 30 | + for (int i=0; i<numNewMessages; i++) { |
| 31 | + String chat_id = String(bot.messages[i].chat_id); |
| 32 | + String text = bot.messages[i].text; |
| 33 | + |
| 34 | + String from_name = bot.messages[i].from_name; |
| 35 | + if (from_name == "") from_name = "Guest"; |
| 36 | + |
| 37 | + if (text == "/send_test_action") { |
| 38 | + bot.sendChatActionMessage(chat_id, "typing"); |
| 39 | + |
| 40 | + // You can't use own message, just choose from one of bellow |
| 41 | + |
| 42 | + //typing for text messages |
| 43 | + //upload_photo for photos |
| 44 | + //record_video or upload_video for videos |
| 45 | + //record_audio or upload_audio for audio files |
| 46 | + //upload_document for general files |
| 47 | + //find_location for location data |
| 48 | + |
| 49 | + //more info here - https://core.telegram.org/bots/api#sendchataction |
| 50 | + } |
| 51 | + |
| 52 | + if (text == "/start") { |
| 53 | + String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n"; |
| 54 | + welcome += "This is Chat Action Bot example.\n\n"; |
| 55 | + welcome += "/send_test_action : to send test chat action message\n"; |
| 56 | + bot.sendMessage(chat_id, welcome); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +void setup() { |
| 63 | + Serial.begin(115200); |
| 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.disconnect(); |
| 69 | + delay(100); |
| 70 | + |
| 71 | + // attempt to connect to Wifi network: |
| 72 | + Serial.print("Connecting Wifi: "); |
| 73 | + Serial.println(ssid); |
| 74 | + WiFi.begin(ssid, password); |
| 75 | + |
| 76 | + while (WiFi.status() != WL_CONNECTED) { |
| 77 | + Serial.print("."); |
| 78 | + delay(500); |
| 79 | + } |
| 80 | + |
| 81 | + Serial.println(""); |
| 82 | + Serial.println("WiFi connected"); |
| 83 | + Serial.print("IP address: "); |
| 84 | + Serial.println(WiFi.localIP()); |
| 85 | +} |
| 86 | + |
| 87 | +void loop() { |
| 88 | + if (millis() > Bot_lasttime + Bot_mtbs) { |
| 89 | + int numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 90 | + |
| 91 | + while(numNewMessages) { |
| 92 | + Serial.println("got response"); |
| 93 | + handleNewMessages(numNewMessages); |
| 94 | + numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 95 | + } |
| 96 | + |
| 97 | + Bot_lasttime = millis(); |
| 98 | + } |
| 99 | +} |
0 commit comments