getUpdatesCLI.php 2.11 KB
Newer Older
MBoretto's avatar
MBoretto committed
1 2
#!/usr/bin/env php
<?php
MBoretto's avatar
MBoretto committed
3 4
//README
//This configuration file is intented to run the bot with the webhook method
5
//Uncommented parameters must be filled
MBoretto's avatar
MBoretto committed
6

MBoretto's avatar
MBoretto committed
7 8 9
#bash script
#while true; do ./getUpdatesCLI.php; done

10 11
// Load composer
require __DIR__ . '/vendor/autoload.php';
MBoretto's avatar
MBoretto committed
12 13 14

$API_KEY = 'your_bot_api_key';
$BOT_NAME = 'namebot';
15 16 17 18 19 20
//$commands_path = __DIR__ . '/Commands/';
$mysql_credentials = [
   'host'     => 'localhost',
   'user'     => 'dbuser',
   'password' => 'dbpass',
   'database' => 'dbname',
MBoretto's avatar
MBoretto committed
21
];
MBoretto's avatar
MBoretto committed
22 23

try {
24
    // Create Telegram API object
MBoretto's avatar
MBoretto committed
25 26
    $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);

27 28
    // Enable MySQL
    $telegram->enableMySQL($mysql_credentials);
MBoretto's avatar
MBoretto committed
29

30 31
    //// Enable MySQL with table prefix
    //$telegram->enableMySQL($mysql_credentials, $BOT_NAME . '_');
MBoretto's avatar
MBoretto committed
32

33 34 35 36
    //// Add an additional commands path
    //$telegram->addCommandsPath($commands_path);

    //// Here you can enable admin interface for the channel you want to manage
MBoretto's avatar
MBoretto committed
37
    //$telegram->enableAdmins(['your_telegram_id']);
38
    //$telegram->setCommandConfig('sendtochannel', ['your_channel' => '@type_here_your_channel']);
MBoretto's avatar
MBoretto committed
39

40 41 42
    //// Here you can set some command specific parameters,
    //// for example, google geocode/timezone api key for date command:
    //$telegram->setCommandConfig('date', ['google_api_key' => 'your_google_api_key_here']);
MBoretto's avatar
MBoretto committed
43

44
    //// Logging
MBoretto's avatar
MBoretto committed
45
    //$telegram->setLogRequests(true);
46
    //$telegram->setLogPath($BOT_NAME . '.log');
MBoretto's avatar
MBoretto committed
47
    //$telegram->setLogVerbosity(3);
MBoretto's avatar
MBoretto committed
48

49 50 51
    //// Set custom Upload and Download path
    //$telegram->setDownloadPath('../Download');
    //$telegram->setUploadPath('../Upload');
MBoretto's avatar
MBoretto committed
52

53
    // Handle telegram getUpdate request
MBoretto's avatar
MBoretto committed
54 55 56 57
    $ServerResponse = $telegram->handleGetUpdates();

    if ($ServerResponse->isOk()) {
        $n_update = count($ServerResponse->getResult());
58
        print(date('Y-m-d H:i:s', time()) . ' - Processed ' . $n_update . ' updates' . "\n");
MBoretto's avatar
MBoretto committed
59
    } else {
60 61
        print(date('Y-m-d H:i:s', time()) . ' - Failed to fetch updates' . "\n");
        echo $ServerResponse->printError() . "\n";
MBoretto's avatar
MBoretto committed
62
    }
MBoretto's avatar
MBoretto committed
63 64
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // log telegram errors
65
    echo $e;
MBoretto's avatar
MBoretto committed
66
}