forked from stgodjah/TelegaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetUpdatesCLI.php
More file actions
63 lines (51 loc) · 2.47 KB
/
getUpdatesCLI.php
File metadata and controls
63 lines (51 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env php
<?php
// Load composer
require_once __DIR__ . '/vendor/autoload.php';
// Load all configuration options
/** @var array $config */
$config = require __DIR__ . '/config.php';
try {
// Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($config['api_key'], $config['bot_username']);
$telegram->useGetUpdatesWithoutDatabase();
// Enable admin users
$telegram->enableAdmins($config['admins']);
// Add commands paths containing your custom commands
$telegram->addCommandsPaths($config['commands']['paths']);
// Enable MySQL if required
$telegram->enableMySql($config['mysql']);
// Logging (Error, Debug and Raw Updates)
// https://github.com/php-telegram-bot/core/blob/master/doc/01-utils.md#logging
//
// (this example requires Monolog: composer require monolog/monolog)
Longman\TelegramBot\TelegramLog::initialize(
new Monolog\Logger('telegram_bot', [
(new Monolog\Handler\StreamHandler($config['logging']['debug'], Monolog\Logger::DEBUG))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
(new Monolog\Handler\StreamHandler($config['logging']['error'], Monolog\Logger::ERROR))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
]),
new Monolog\Logger('telegram_bot_updates', [
(new Monolog\Handler\StreamHandler($config['logging']['update'], Monolog\Logger::INFO))->setFormatter(new Monolog\Formatter\LineFormatter('%message%' . PHP_EOL)),
])
);
// Set custom Download and Upload paths
$telegram->setDownloadPath($config['paths']['download']);
$telegram->setUploadPath($config['paths']['upload']);
// Handle telegram getUpdates request
$server_response = $telegram->handleGetUpdates();
if ($server_response->isOk()) {
$update_count = count($server_response->getResult());
echo date('Y-m-d H:i:s') . ' - Processed ' . $update_count . ' updates';
} else {
echo date('Y-m-d H:i:s') . ' - Failed to fetch updates' . PHP_EOL;
echo $server_response->printError();
}
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// Log telegram errors
Longman\TelegramBot\TelegramLog::error($e);
// Uncomment this to output any errors (ONLY FOR DEVELOPMENT!)
echo $e;
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
// Uncomment this to output log initialisation errors (ONLY FOR DEVELOPMENT!)
echo $e;
}