commit b6b01b48dc01a41db14066196fe3d379560d67f0 Author: Frederico @ VilaRosa02 Date: Wed Aug 27 08:52:36 2025 +0000 init diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5ccabf4 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +include .env +export +SHELL=/bin/bash + + +ALL_UPDATE_IDS_SRC=$(shell ls data/*.update_id.json) +ALL_UPDATE_IDS=$(subst .update_id,.processed_id,$(ALL_UPDATE_IDS_SRC)) + + + +process_updates: $(ALL_UPDATE_IDS) + +%.processed_id.json: %.update_id.json process_message.php + @echo " $@ + + +fetch_updates: + curl -s "https://api.telegram.org/bot$(TELEGRAM_BOT_TOKEN)/getUpdates" \ + | jq -c '.result[]' \ + | while read -r update_msg; \ + do \ + echo $$update_msg > data/$$(echo $$update_msg | jq -c '.update_id').update_id.json; \ + done + +clean: + rm data/*.json diff --git a/lib/main.php b/lib/main.php new file mode 100644 index 0000000..6a94218 --- /dev/null +++ b/lib/main.php @@ -0,0 +1,3 @@ + $chatId, + 'text' => $text, + 'parse_mode' => $parseMode, + 'disable_web_page_preview' => true, + ]; + return telegram_post($url, $payload); +} + +/** + * $kind: 'photo' or 'document' + * - photo → shows as image preview + * - document → shows as file (any type: pdf/zip/etc.) + */ +function telegram_send_file(string $chatId, string $filePath, string $kind = 'document', ?string $caption = null, string $parseMode = 'HTML'): array { + global $TELEGRAM_BOT_TOKEN; + if (!file_exists($filePath)) { + throw new RuntimeException("File not found: $filePath"); + } + + $method = $kind === 'photo' ? 'sendPhoto' : 'sendDocument'; + $field = $kind === 'photo' ? 'photo' : 'document'; + $url = 'https://api.telegram.org/bot'.$TELEGRAM_BOT_TOKEN.'/'.$method; + + $payload = [ + 'chat_id' => $chatId, + $field => new CURLFile(realpath($filePath)), + ]; + if ($caption !== null) { + $payload['caption'] = $caption; + $payload['parse_mode'] = $parseMode; + } + + return telegram_post($url, $payload, /*multipart=*/true); +} + +/* ---- internals ---- */ +function telegram_post(string $url, array $payload, bool $multipart = false): array { + global $TELEGRAM_BOT_TOKEN; + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POSTFIELDS => $payload, // if contains CURLFile, cURL uses multipart automatically + ]); + + $resp = curl_exec($ch); + if ($resp === false) { + $err = curl_error($ch); + curl_close($ch); + throw new RuntimeException("cURL error: $err"); + } + curl_close($ch); + + $data = json_decode($resp, true); + if (!is_array($data) || empty($data['ok'])) { + throw new RuntimeException("Telegram error: $resp"); + } + return $data; +} diff --git a/process_message.php b/process_message.php new file mode 100644 index 0000000..eac1c46 --- /dev/null +++ b/process_message.php @@ -0,0 +1,36 @@ +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? + + + +} +