IMPROVEMENT: now telegram knows about the available commands. each command is a php file
This commit is contained in:
parent
0f52e28b7e
commit
d5bdf67ce5
13
Makefile
13
Makefile
@ -9,11 +9,12 @@ all: fetch_updates process_updates
|
|||||||
|
|
||||||
process_updates: $(ALL_UPDATE_IDS)
|
process_updates: $(ALL_UPDATE_IDS)
|
||||||
|
|
||||||
%.processed_id.json: %.update_id.json process_message.php
|
%.processed_id.json: %.update_id.json router.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 > $@
|
cat $< | php router.php > $@
|
||||||
|
|
||||||
|
|
||||||
fetch_updates:
|
fetch_updates:
|
||||||
|
echo "Fetching new updates..."
|
||||||
curl -s "https://api.telegram.org/bot$(TELEGRAM_BOT_TOKEN)/getUpdates" \
|
curl -s "https://api.telegram.org/bot$(TELEGRAM_BOT_TOKEN)/getUpdates" \
|
||||||
| jq -c '.result[]' \
|
| jq -c '.result[]' \
|
||||||
| while read -r update_msg; \
|
| while read -r update_msg; \
|
||||||
@ -30,5 +31,13 @@ fetch_updates:
|
|||||||
fi; \
|
fi; \
|
||||||
done
|
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:
|
clean:
|
||||||
rm data/*.json
|
rm data/*.json
|
||||||
|
|||||||
38
commands/command_1.sample.php
Normal file
38
commands/command_1.sample.php
Normal 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");
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
38
commands/command_2.sample.php
Normal file
38
commands/command_2.sample.php
Normal 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");
|
||||||
|
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
@ -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
28
router.php
Normal 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);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user