init
This commit is contained in:
commit
b6b01b48dc
26
Makefile
Normal file
26
Makefile
Normal file
@ -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 "<?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 > $@
|
||||
|
||||
|
||||
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
|
||||
3
lib/main.php
Normal file
3
lib/main.php
Normal file
@ -0,0 +1,3 @@
|
||||
<?php
|
||||
$TELEGRAM_BOT_TOKEN = getenv('TELEGRAM_BOT_TOKEN');
|
||||
require __DIR__."/telegram_lib.php";
|
||||
64
lib/telegram_lib.php
Normal file
64
lib/telegram_lib.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
function telegram_send_text(string $chatId, string $text, string $parseMode = 'HTML'): array {
|
||||
global $TELEGRAM_BOT_TOKEN;
|
||||
$url = 'https://api.telegram.org/bot'.$TELEGRAM_BOT_TOKEN.'/sendMessage';
|
||||
$payload = [
|
||||
'chat_id' => $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;
|
||||
}
|
||||
36
process_message.php
Normal file
36
process_message.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?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?
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user