forked from php-telegram-bot/example-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.php
89 lines (73 loc) · 3.05 KB
/
cron.php
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* README
* This configuration file is intended to run a list of commands with crontab.
* Uncommented parameters must be filled
*/
// Your command(s) to run, pass it just like in a message (arguments supported)
$commands = [
'/whoami',
"/echo I'm a bot!",
];
// Load composer
require_once __DIR__ . '/vendor/autoload.php';
// Add you bot's API key and name
$bot_api_key = 'your:bot_api_key';
$bot_username = 'username_bot';
// Define all IDs of admin users in this array (leave as empty array if not used)
$admin_users = [
// 123,
];
// Define all paths for your custom commands in this array (leave as empty array if not used)
$commands_paths = [
// __DIR__ . '/Commands/',
];
// Enter your MySQL database credentials
//$mysql_credentials = [
// 'host' => 'localhost',
// 'user' => 'dbuser',
// 'password' => 'dbpass',
// 'database' => 'dbname',
//];
try {
// Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
// Add commands paths containing your custom commands
$telegram->addCommandsPaths($commands_paths);
// Enable admin users
$telegram->enableAdmins($admin_users);
// Enable MySQL
//$telegram->enableMySql($mysql_credentials);
// 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(__DIR__ . "/{$bot_username}_debug.log", Monolog\Logger::DEBUG))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
// (new Monolog\Handler\StreamHandler(__DIR__ . "/{$bot_username}_error.log", Monolog\Logger::ERROR))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
// ]),
// new Monolog\Logger('telegram_bot_updates', [
// (new Monolog\Handler\StreamHandler(__DIR__ . "/{$bot_username}_update.log", Monolog\Logger::INFO))->setFormatter(new Monolog\Formatter\LineFormatter('%message%' . PHP_EOL)),
// ])
//);
// Set custom Upload and Download paths
//$telegram->setDownloadPath(__DIR__ . '/Download');
//$telegram->setUploadPath(__DIR__ . '/Upload');
// Here you can set some command specific parameters,
// e.g. Google geocode/timezone api key for /date command:
//$telegram->setCommandConfig('date', ['google_api_key' => 'your_google_api_key_here']);
// Requests Limiter (tries to prevent reaching Telegram API limits)
$telegram->enableLimiter();
// Run user selected commands
$telegram->runCommands($commands);
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// Silence is golden!
//echo $e;
// Log telegram errors
Longman\TelegramBot\TelegramLog::error($e);
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
// Silence is golden!
// Uncomment this to catch log initialisation errors
//echo $e;
}