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);
+}