Telegram: add command announcement via code

This commit is contained in:
Marcel Walter 2026-01-21 10:29:23 +01:00
parent ec8f14ea01
commit 2121251553
3 changed files with 43 additions and 9 deletions

View File

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

View File

@ -1,5 +1,6 @@
#include <Telegram.h>
#include "secrets.h"
#include <ArduinoJson.h>
const char *Telegram::BOT_TOKEN = TELEGRAM_BOT_TOKEN;
const unsigned long Telegram::BOT_MTBS = 1000; // mean time between scan messages
@ -111,6 +112,33 @@ void Telegram::init()
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
JsonDocument doc;
JsonArray commandsJsonArray = doc.to<JsonArray>();
String jsonString;
// add all the commands to the list
for (uint32_t i = 0; i < cmdListCount; i++)
{
auto cmdObj = commandsJsonArray.add<JsonObject>();
// remove the prepending slash as it is not needed here
String tmp = cmdList[i].command;
tmp.remove(0,1);
cmdObj["command"] = tmp;
cmdObj["description"] = cmdList[i].description;
}
// add generic commands to the list
auto helpObj = commandsJsonArray.add<JsonObject>();
helpObj["command"] = "help";
helpObj["description"] = "Zeigt einen Hilfstext an";
// auto startObj = commandsJsonArray.add<JsonObject>();
// startObj["command"] = "start";
// startObj["description"] = "Startet die Konversation";
serializeJson(doc, jsonString);
bot.setMyCommands(jsonString);
bot.sendMessage(TELEGRAM_BOT_OWNER, "Hi I'm online.");
bot_lasttime = millis();
}

View File

@ -129,12 +129,17 @@ String printText(String Args);
String printLate15(String Args);
String printLate30(String Args);
#define OPEN_STRING "offen"
#define CLOSED_STRING "leider zu"
#define LATE15_STRING "Wir oeffnen 15 Minuten spaeter"
#define LATE30_STRING "Wir oeffnen 30 Minuten spaeter"
TelegramCommand myCmdList[]{
{"/open", printOpen},
{"/close", printClose},
{"/text", printText},
{"/late_15", printLate15},
{"/late_30", printLate30},
{"/open", printOpen, "Display zeigt \"" OPEN_STRING "\""},
{"/close", printClose, "Display zeigt \"" CLOSED_STRING "\""},
{"/text", printText, "(lange drücken) Display zeigt den Text hinter dem Kommando an"},
{"/late_15", printLate15, "Display zeigt \"" LATE15_STRING "\""},
{"/late_30", printLate30, "Display zeigt \"" LATE30_STRING "\""},
};
uint32_t myCmdListCnt = sizeof(myCmdList) / sizeof(myCmdList[0]);
@ -399,7 +404,7 @@ void displayText(const char *text, bool background, int16_t x, int16_t y)
String printOpen(String Args)
{
Text = "offen";
Text = OPEN_STRING;
publishOpen();
return Text;
@ -407,7 +412,7 @@ String printOpen(String Args)
String printClose(String Args)
{
Text = "leider zu";
Text = CLOSED_STRING;
publishClose();
return Text;
@ -426,7 +431,7 @@ String printText(String Args)
String printLate15(String Args)
{
Text = "Wir oeffnen 15 Minuten spaeter";
Text = LATE15_STRING;
publishClose();
return Text;
@ -434,7 +439,7 @@ String printLate15(String Args)
String printLate30(String Args)
{
Text = "Wir oeffnen 30 Minuten spaeter";
Text = LATE30_STRING;
publishClose();
return Text;