From d5bdf67ce507a92c1c91ed555f6246fd29f1ada5 Mon Sep 17 00:00:00 2001 From: "Frederico @ VilaRosa02" Date: Sun, 31 Aug 2025 17:37:20 +0000 Subject: [PATCH] IMPROVEMENT: now telegram knows about the available commands. each command is a php file --- Makefile | 13 ++++++++++-- commands/command_1.sample.php | 38 +++++++++++++++++++++++++++++++++++ commands/command_2.sample.php | 38 +++++++++++++++++++++++++++++++++++ process_message.sample.php | 36 --------------------------------- router.php | 28 ++++++++++++++++++++++++++ 5 files changed, 115 insertions(+), 38 deletions(-) create mode 100644 commands/command_1.sample.php create mode 100644 commands/command_2.sample.php delete mode 100644 process_message.sample.php create mode 100644 router.php diff --git a/Makefile b/Makefile index f4fbbb1..a5509a2 100644 --- a/Makefile +++ b/Makefile @@ -9,11 +9,12 @@ all: fetch_updates process_updates process_updates: $(ALL_UPDATE_IDS) -%.processed_id.json: %.update_id.json process_message.php - @echo " $@ +%.processed_id.json: %.update_id.json router.php + cat $< | php router.php > $@ fetch_updates: + echo "Fetching new updates..." curl -s "https://api.telegram.org/bot$(TELEGRAM_BOT_TOKEN)/getUpdates" \ | jq -c '.result[]' \ | while read -r update_msg; \ @@ -30,5 +31,13 @@ fetch_updates: fi; \ done +upload-available-commands: data/available-commands.json + curl -s "https://api.telegram.org/bot$(TELEGRAM_BOT_TOKEN)/setMyCommands" -d 'commands=$(shell cat data/available-commands.json)' + echo "New commands uploaded" + + +data/available-commands.json: $(wildcard commands/*.inc.php) + php router.php --print-json-command-list > $@ + clean: rm data/*.json diff --git a/commands/command_1.sample.php b/commands/command_1.sample.php new file mode 100644 index 0000000..7355f12 --- /dev/null +++ b/commands/command_1.sample.php @@ -0,0 +1,38 @@ + "/hello", + "description" => "simple 'hello' world command", + "callback" => function (array $from,array $chat,string $date,string $text) { + // TELEGRAM FUNCTIONS SAMPLE USAGE : + // + // telegram_send_text($chat['id'], "Olá, Fred! Negrito"); + // telegram_send_file($chat['id'], '/path/to/image.jpg', 'photo', 'Here is a photo'); + // telegram_send_file($chat['id'], '/path/to/report.pdf', 'document', 'Monthly report'); + // + // [from] => Array + // ( + // [id] => 122054164 + // [is_bot] => + // [first_name] => Fred + // [last_name] => Flinstone + // [username] => fredflinstone + // [language_code] => en + // [is_premium] => 1 + // ) + // + // [chat] => Array + // ( + // [id] => 122054164 + // [first_name] => Fred + // [last_name] => Flinstone + // [username] => fredflinstone + // [type] => private + // ) + // + // [date] => 1756281243 + // [text] => tudo bem? + + telegram_send_text($chat['id'], "world"); + } +]; + diff --git a/commands/command_2.sample.php b/commands/command_2.sample.php new file mode 100644 index 0000000..ddb135c --- /dev/null +++ b/commands/command_2.sample.php @@ -0,0 +1,38 @@ + "/goodbye", + "description" => "simple goodbye command", + "callback" => function (array $from,array $chat,string $date,string $text) { + // TELEGRAM FUNCTIONS SAMPLE USAGE : + // + // telegram_send_text($chat['id'], "Olá, Fred! Negrito"); + // telegram_send_file($chat['id'], '/path/to/image.jpg', 'photo', 'Here is a photo'); + // telegram_send_file($chat['id'], '/path/to/report.pdf', 'document', 'Monthly report'); + // + // [from] => Array + // ( + // [id] => 122054164 + // [is_bot] => + // [first_name] => Fred + // [last_name] => Flinstone + // [username] => fredflinstone + // [language_code] => en + // [is_premium] => 1 + // ) + // + // [chat] => Array + // ( + // [id] => 122054164 + // [first_name] => Fred + // [last_name] => Flinstone + // [username] => fredflinstone + // [type] => private + // ) + // + // [date] => 1756281243 + // [text] => tudo bem? + telegram_send_text($chat['id'], "goodbye"); + + } +]; + diff --git a/process_message.sample.php b/process_message.sample.php deleted file mode 100644 index eac1c46..0000000 --- a/process_message.sample.php +++ /dev/null @@ -1,36 +0,0 @@ -Negrito"); - // telegram_send_file('479054164', '/path/to/image.jpg', 'photo', 'Here is a photo'); - // telegram_send_file('479054164', '/path/to/report.pdf', 'document', 'Monthly report'); - // - // [from] => Array - // ( - // [id] => 479054164 - // [is_bot] => - // [first_name] => Frederico - // [last_name] => Falcão - // [username] => fredericomfalcao - // [language_code] => en - // [is_premium] => 1 - // ) - // - // [chat] => Array - // ( - // [id] => 479054164 - // [first_name] => Frederico - // [last_name] => Falcão - // [username] => fredericomfalcao - // [type] => private - // ) - // - // [date] => 1756281243 - // [text] => tudo bem? - - - -} - diff --git a/router.php b/router.php new file mode 100644 index 0000000..95a31c9 --- /dev/null +++ b/router.php @@ -0,0 +1,28 @@ + 1 && $argv[1] == "--print-json-command-list") { + $out = []; foreach($commands as $k => $v) $out[] = ["command"=>$v["command"],"description"=>$v["description"]]; + echo json_encode(array_values($out)); + exit(0); +} + +// 2. PROCESS COMMAND +extract(json_decode(file_get_contents('php://stdin'),1)["message"]); // from, chat, date, text +if (isset($commands[$text])) { + $commands[$text]["callback"]($from, $chat, $date, $text); +}