#include #include "secrets.h" const char *Telegram::BOT_TOKEN = TELEGRAM_BOT_TOKEN; const unsigned long Telegram::BOT_MTBS = 1000; // mean time between scan messages void Telegram::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?"); // You can't use own message, just choose from one of bellow // typing for text messages // upload_photo for photos // record_video or upload_video for videos // record_audio or upload_audio for audio files // upload_document for general files // find_location for location data // more info here - https://core.telegram.org/bots/api#sendchataction } for (uint32_t i = 0; i < cmdListCount; i++) { if (text.startsWith(cmdList[i].command)) { String Arguments = ""; if (text.length() > cmdList[i].command.length() + 1) { // Arguments are on space behind the command string Arguments = text.substring(cmdList[i].command.length() + 1); } if (cmdList[i].functionPointer != nullptr) { String DisplayText = cmdList[i].functionPointer(Arguments); if (DisplayText.isEmpty()) { bot.sendMessage(chat_id, "Display text is empty!!"); } else { String Answer = "Display text is \""; Answer += DisplayText; Answer += "\"."; bot.sendMessage(chat_id, "Display text is empty!!"); } break; } } } if ((text == "/display_start") || (text == "/start") || (text == "/help")) { String welcome = "Willkommen beim Tür-Display-Bot, " + from_name + ".\n\n"; welcome += "Hier kommen die möglichen Kommandos, die ich verstehe:\n"; for (uint32_t i = 0; i < cmdListCount; i++) { welcome += cmdList[i].command + "\n"; } bot.sendMessage(chat_id, welcome); } } } Telegram::Telegram(WiFiClientSecure &sec_client, TelegramCommand commandList[], uint32_t commandListCount) : secured_client(sec_client), bot(BOT_TOKEN, sec_client), cmdList(commandList), cmdListCount(commandListCount) { } Telegram::~Telegram() {} void Telegram::init() { secured_client.setInsecure(); secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org secured_client.setHandshakeTimeout(5); // secured_client.setTimeout(5); Serial.println("clearing old messages"); int numNewMessages = bot.getUpdates(bot.last_message_received + 1); while (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; String txt = String("ignored '") + text + String("' from ") + from_name +String("(") + chat_id + String(")"); Serial.println(txt); } numNewMessages = bot.getUpdates(bot.last_message_received + 1); } bot.sendMessage(TELEGRAM_BOT_OWNER, "Hi I'm online."); bot_lasttime = millis(); } void Telegram::cyclic(unsigned long now) { if (now - bot_lasttime > BOT_MTBS) { Serial.print(bot.last_message_received); Serial.println(" checking"); auto stat = WiFi.status(); Serial.println("WifiStat= "); Serial.println(stat); int numNewMessages = bot.getUpdates(bot.last_message_received + 1); while (numNewMessages) { handleNewMessages(numNewMessages); numNewMessages = bot.getUpdates(bot.last_message_received + 1); } bot_lasttime = now; } }