Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
- #include <WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #define BOT_TOKEN "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
- const unsigned long BOT_MTBS = 1000;
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- unsigned long bot_lasttime;
- bool Start = false;
- void handleNewMessages(int numNewMessages)
- {
- Serial.println("handleNewMessages");
- Serial.println(String(numNewMessages));
- for (int i = 0; i < numNewMessages; i++)
- {
- String chat_id = bot.messages[i].chat_id;
- String text = bot.messages[i].text;
- String from_name = bot.messages[i].from_name;
- if (from_name == "")
- from_name = "Guest";
- if (text == "/send_test_action")
- {
- bot.sendChatAction(chat_id, "typing");
- delay(4000);
- bot.sendMessage(chat_id, "Did you see the action message?");
- }
- if (text == "/start")
- {
- String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
- welcome += "This is Chat Action Bot example.\n\n";
- welcome += "/send_test_action : to send test chat action message\n";
- bot.sendMessage(chat_id, welcome);
- }
- }
- }
- void setup() {
- Serial.begin(115200);
- Serial.println();
- Connect_To_WiFi();
- Connect_To_Telegram();
- }
- void loop() {
- ReConnect_To_WiFi();
- handel_messages_from_Telegram_Users();
- }
- void ReConnect_To_WiFi() {
- while (WiFi.status() != WL_CONNECTED) {
- //delay(500);
- Serial.print(".");
- Connect_To_WiFi();
- }
- }
- void Connect_To_WiFi() {
- WiFiManager wm;
- bool res;
- res = wm.autoConnect("Senior Project [Setup WiFi]"); // password protected ap
- if (!res) {
- Serial.println("Failed to Connect!!");
- // ESP.restart();
- }
- else {
- //if you get here you have connected to the WiFi
- Serial.println("Connected...");
- }
- }
- void handel_messages_from_Telegram_Users() {
- if (millis() - bot_lasttime > BOT_MTBS)
- {
- int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- while (numNewMessages)
- {
- Serial.println("got response");
- handleNewMessages(numNewMessages);
- numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- }
- bot_lasttime = millis();
- }
- }
- void Connect_To_Telegram() {
- secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
- Serial.print("Retrieving time: ");
- configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
- time_t now = time(nullptr);
- while (now < 24 * 3600)
- {
- Serial.print(".");
- delay(100);
- now = time(nullptr);
- }
- Serial.println(now);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement