IMPROVEMENT: now telegram knows about the available commands. each command is a php file

This commit is contained in:
Frederico @ VilaRosa02 2025-08-31 17:37:20 +00:00
parent 0f52e28b7e
commit d5bdf67ce5
5 changed files with 115 additions and 38 deletions

View File

@ -9,11 +9,12 @@ all: fetch_updates process_updates
process_updates: $(ALL_UPDATE_IDS)
%.processed_id.json: %.update_id.json process_message.php
@echo "<?php require __DIR__.'/lib/main.php'; require 'process_message.php'; extract(json_decode(file_get_contents('$<'),1)[\"message\"]); process_message(\$$from, \$$chat, \$$date, \$$text);" | php > $@
%.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

View File

@ -0,0 +1,38 @@
<?php
return [
"command" => "/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! <b>Negrito</b>");
// 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");
}
];

View File

@ -0,0 +1,38 @@
<?php
return [
"command" => "/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! <b>Negrito</b>");
// 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");
}
];

View File

@ -1,36 +0,0 @@
<?php
function process_message(array $from,array $chat,string $date,string $text) {
// TELEGRAM FUNCTIONS SAMPLE USAGE :
//
// telegram_send_text('479054164', "Olá, Frederico! <b>Negrito</b>");
// 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?
}

28
router.php Normal file
View File

@ -0,0 +1,28 @@
<?php
require __DIR__.'/lib/main.php';
// Build all the commands array
$commands = [];
foreach(scandir("commands") as $f) {
if ($f == "." || $f == "..") continue;
$filepath = "commands/$f";
if (substr($f,-8) == ".inc.php") {
$array = require $filepath;
$commands[$array["command"]] = $array;
}
}
// 1. PRINT JSON COMMAND LIST
if ($argc > 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);
}