implementation

This commit is contained in:
小喵 2023-03-17 14:53:13 +08:00
parent 697f3cba67
commit d157e331cc
Signed by: mt
GPG key ID: 2BCF198BD3341FB3
5 changed files with 92 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
config.ini

1
.idea/php.xml generated
View file

@ -10,6 +10,7 @@
<option name="highlightLevel" value="WARNING" /> <option name="highlightLevel" value="WARNING" />
<option name="transferred" value="true" /> <option name="transferred" value="true" />
</component> </component>
<component name="PhpProjectSharedConfiguration" php_language_level="8.2" />
<component name="PhpStanOptionsConfiguration"> <component name="PhpStanOptionsConfiguration">
<option name="transferred" value="true" /> <option name="transferred" value="true" />
</component> </component>

18
config-example.ini Normal file
View file

@ -0,0 +1,18 @@
; Each bot is given a unique authentication token when it is created.
; The token looks something like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11.
; You can learn about obtaining tokens and generating new ones in the Telegram Bot API document.
token =
; The Telegram Bot API endpoint. Change this when you want to use your own Bot API server.
endpoint = https://api.telegram.org/bot
; The file that saves the last update id to tell Telegram that we already processed the updates.
last_update_id_file = last-update-id.txt
; Limits the number of updates to be retrieved. Values between 1-100 are accepted. Defaults to 100.
limit = 100
; Timeout in seconds for long polling.
; Defaults to 0, i.e. usual short polling.
; Should be positive, short polling should be used for testing purposes only.
timeout = 60

1
last-update-id.txt Normal file
View file

@ -0,0 +1 @@
952645275

71
main.php Executable file
View file

@ -0,0 +1,71 @@
#!/usr/bin/env php
<?php
$configFilename = __DIR__ . '/config.ini';
if (!file_exists($configFilename)) {
error_log('Failed to read config.ini file. You may copy from config-example.ini and edit it.');
exit(1);
}
$config = parse_ini_file($configFilename);
// save and load last update id
$getLastUpdateId = fn() => file_exists($config['last_update_id_file']) ? (int)file_get_contents($config['last_update_id_file']) : 0;
$putLastUpdateId = fn($lastUpdateId) => file_put_contents($config['last_update_id_file'], "$lastUpdateId");
// request Telegram Bot API
$requestApi = fn(string $method, ?array $payload = null) => json_decode(file_get_contents(
"$config[endpoint]$config[token]/$method",
false,
stream_context_create(['http' => [
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => json_encode($payload),
]]),
), true);
// some method we are using
$getMe = fn() => $requestApi('getMe');
$getUpdates = fn(int $offset = 0) => $requestApi('getUpdates', [
'offset' => $offset,
'limit' => (int)$config['limit'],
'timeout' => (int)$config['timeout'],
]);
$sendMessage = fn($params) => $requestApi('sendMessage', $params);
$response = $getMe();
if (!$response['ok']) {
error_log('Failed to start the bot.');
error_log("Error message: $response[description]");
error_log("Error code: $response[error_code]");
exit(1);
}
$me = $response['result'];
error_log('Login as: ' . json_encode($me, JSON_UNESCAPED_UNICODE));
error_log('Start receiving updates…');
while (($response = $getUpdates($getLastUpdateId() + 1))['ok']) {
$updates = $response['result'];
$lastUpdateId = null;
foreach ($updates as $update) {
$lastUpdateId = $update['update_id'];
error_log('Got an update: ' . json_encode($update, JSON_UNESCAPED_UNICODE));
if (isset($update['message'])) {
$sendMessage([
'chat_id' => $update['message']['chat']['id'],
'text' => '喵',
'reply_to_message_id' => $update['message']['message_id'],
]);
}
$putLastUpdateId($lastUpdateId);
}
}
if (!$response['ok']) {
error_log('Failed to get updates.');
error_log("Error message: $response[description]");
error_log("Error code: $response[error_code]");
exit(1);
}