add Telegram response to each command with display text

This commit is contained in:
Marcel Walter 2026-01-14 23:13:06 +01:00
parent 7c316e54cc
commit f4fa4b7815
3 changed files with 34 additions and 18 deletions

View File

@ -6,7 +6,7 @@
struct TelegramCommand
{
const String command;
void (*const functionPointer) (const String arguments);
String (*const functionPointer) (const String arguments);
};
class Telegram

View File

@ -43,22 +43,32 @@ void Telegram::handleNewMessages(int numNewMessages)
String Arguments = "";
if (text.length() > cmdList[i].command.length() + 1)
{
//Arguments are on space behind the command string
// Arguments are on space behind the command string
Arguments = text.substring(cmdList[i].command.length() + 1);
}
if (cmdList[i].functionPointer != nullptr)
{
cmdList[i].functionPointer(Arguments);
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;
}
break;
}
}
if (text == "/display_start")
if ((text == "/display_start") || (text == "/start") || (text == "/help"))
{
String welcome = "Willkommen beim Tür-Display-Bot," + from_name + ".\n";
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++)

View File

@ -35,15 +35,15 @@ void displayText(const char *text, bool background, int16_t x = -1, int16_t y =
// BearSSL::WiFiClientSecure secured_client;
WiFiClientSecure secured_client;
// TelegramCommand callbacks must fulfill a function pointer: void (*) (const String arguments)
void printOpen(String Args);
void printClose(String Args);
void printText(String Args);
// TelegramCommand callbacks must fulfill a function pointer: String (*) (const String arguments)
String printOpen(String Args);
String printClose(String Args);
String printText(String Args);
TelegramCommand myCmdList[]{
{"/display_open", printOpen},
{"/display_close", printClose},
{"/display_text", printText},
{"/open", printOpen},
{"/close", printClose},
{"/text", printText},
};
uint32_t myCmdListCnt = sizeof(myCmdList) / sizeof(myCmdList[0]);
@ -288,20 +288,26 @@ void displayText(const char *text, bool background, int16_t x, int16_t y)
canvas.print(text);
}
void printOpen(String Args)
String printOpen(String Args)
{
Text = "offen";
return Text;
}
void printClose(String Args)
String printClose(String Args)
{
Text = "leider zu";
return Text;
}
void printText(String Args)
String printText(String Args)
{
Serial.print("Display would show '");
Serial.print(Args);
Serial.println("'");
Text = Args;
return Text;
}