diff --git a/include/ITrigger.h b/include/ITrigger.h deleted file mode 100644 index 71193dc..0000000 --- a/include/ITrigger.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include - -class ITrigger -{ -public: - ITrigger(/* args */) = default; - ~ITrigger() = default; - - virtual void printText(String text) = 0; - virtual void printOpen() = 0; - virtual void printClose() = 0; -}; - \ No newline at end of file diff --git a/include/Telegram.h b/include/Telegram.h index 88fce14..1f53762 100644 --- a/include/Telegram.h +++ b/include/Telegram.h @@ -3,7 +3,11 @@ #include #include -#include "ITrigger.h" +struct TelegramCommand +{ + const String command; + void (*const functionPointer) (const String arguments); +}; class Telegram { @@ -13,14 +17,15 @@ private: unsigned long bot_lasttime; // last time messages' scan has been done - ITrigger &trigger; WiFiClientSecure &secured_client; UniversalTelegramBot bot; + TelegramCommand *cmdList; + uint32_t cmdListCount; void handleNewMessages(int numNewMessages); public: - Telegram(WiFiClientSecure &sec_client, ITrigger &trig); + Telegram(WiFiClientSecure &sec_client, TelegramCommand commandList[], uint32_t commandListCount); ~Telegram(); void init(); diff --git a/src/Telegram.cpp b/src/Telegram.cpp index 9bc50f7..be68d9a 100644 --- a/src/Telegram.cpp +++ b/src/Telegram.cpp @@ -36,41 +36,44 @@ void Telegram::handleNewMessages(int numNewMessages) // more info here - https://core.telegram.org/bots/api#sendchataction } - if (text == "/send_test_action2") + for (uint32_t i = 0; i < cmdListCount; i++) { - bot.sendChatAction(chat_id, "find_location"); - delay(4000); - bot.sendMessage(chat_id, "Did you see the action message?"); - } + 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 (text.startsWith("/txt ")) - { - trigger.printText(text.substring(5)); - } + if (cmdList[i].functionPointer != nullptr) + { + cmdList[i].functionPointer(Arguments); + } - if (text.startsWith("/open")) - { - trigger.printOpen(); + break; + } } - - if (text.startsWith("/close")) + + if (text == "/display_start") { - trigger.printClose(); - } + String welcome = "Willkommen beim Tür-Display-Bot," + from_name + ".\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"; + } - 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"; - welcome += "/send_test_action2 : to send test chat action message\n"; bot.sendMessage(chat_id, welcome); } } } -Telegram::Telegram(WiFiClientSecure &sec_client, ITrigger &trig) - : trigger(trig), secured_client(sec_client), bot(BOT_TOKEN, sec_client) {} +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() {} diff --git a/src/main.cpp b/src/main.cpp index c14655d..7be3817 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,48 +1,50 @@ #include #include #include +#include #include "Telegram.h" -#include "ITrigger.h" #include "FlipDotDrv.h" #include "secrets.h" +bool OTAStarted; +void onOTAStart() { + OTAStarted = true; +} + // BearSSL::WiFiClientSecure secured_client; WiFiClientSecure secured_client; bool Start = false; FlipDotDrv flip(96, 16, 1); -class myTrigger : public ITrigger +void printOpen(String Args) { -private: - /* data */ -public: - myTrigger(/* args */) = default; - ~myTrigger() = default; + flip.printText("offen", 18, 0, 2); +} - void printText(String text) override - { - Serial.print("Display would show '"); - Serial.print(text); - Serial.println("'"); - flip.printText(text.c_str()); - } +void printClose(String Args) +{ + flip.printText("geschlossen", 15, 4, 1); +} - void printOpen() override - { - flip.printText("offen", 18, 0, 2); - } +void printText(String Args) +{ + Serial.print("Display would show '"); + Serial.print(Args); + Serial.println("'"); + flip.printText(Args.c_str()); +} - void printClose() override - { - flip.printText("geschlossen", 15, 4, 1); - } +TelegramCommand myCmdList[] { + {"/display_open", printOpen}, + {"/display_close", printClose}, + {"/display_text", printText}, +}; -} trigg; +uint32_t myCmdListCnt = sizeof(myCmdList) / sizeof(myCmdList[0]); - -Telegram tele(secured_client, trigg); +Telegram tele(secured_client, myCmdList, myCmdListCnt); uint8_t rawBuff[192] = {};