Commit 4e511e9d authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge pull request #382 from jacklul/crontab

Crontab-friendly script
parents 284b1e95 63a948dd
<?php
/**
* README
* This configuration file is intended to run your commands with crontab.
* Uncommented parameters must be filled
*/
// Load composer
require __DIR__ . '/vendor/autoload.php';
// Add you bot's API key and name
$API_KEY = 'your_bot_api_key';
$BOT_NAME = 'username_bot';
// Define a path for your custom commands
//$commands_path = __DIR__ . '/Commands/';
// Enter your MySQL database credentials
//$mysql_credentials = [
// 'host' => 'localhost',
// 'user' => 'dbuser',
// 'password' => 'dbpass',
// 'database' => 'dbname',
//];
// Your command(s) to run, pass it just like in a message (arguments supported)
$commands = ['/whoami', '/echo I\'m a bot!'];
try {
// Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
// Error, Debug and Raw Update logging
//Longman\TelegramBot\TelegramLog::initialize($your_external_monolog_instance);
//Longman\TelegramBot\TelegramLog::initErrorLog($path . '/' . $BOT_NAME . '_error.log');
//Longman\TelegramBot\TelegramLog::initDebugLog($path . '/' . $BOT_NAME . '_debug.log');
//Longman\TelegramBot\TelegramLog::initUpdateLog($path . '/' . $BOT_NAME . '_update.log');
// Enable MySQL
//$telegram->enableMySql($mysql_credentials);
// Enable MySQL with table prefix
//$telegram->enableMySql($mysql_credentials, $BOT_NAME . '_');
// Add an additional commands path
//$telegram->addCommandsPath($commands_path);
// Enable admin user(s)
//$telegram->enableAdmin(your_telegram_id);
//$telegram->enableAdmins([your_telegram_id, other_telegram_id]);
// Add the channel you want to manage
//$telegram->setCommandConfig('sendtochannel', ['your_channel' => '@type_here_your_channel']);
// 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']);
// Set custom Upload and Download path
//$telegram->setDownloadPath('../Download');
//$telegram->setUploadPath('../Upload');
// 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 initilization errors
//echo $e;
}
......@@ -830,4 +830,57 @@ class Telegram
return $this;
}
/**
* Run provided commands
*
* @param array $commands
*
* @throws TelegramException
*/
public function runCommands($commands)
{
if (!is_array($commands) || empty($commands)) {
throw new TelegramException('No command(s) provided!');
}
$this->botan_enabled = false; // Force disable Botan.io integration, we don't want to track self-executed commands!
$result = Request::getMe()->getResult();
if (!$result->getId()) {
throw new TelegramException('Received empty/invalid getMe result!');
}
$bot_id = $result->getId();
$bot_name = $result->getFirstName();
$bot_username = $result->getUsername();
$this->enableAdmin($bot_id); // Give bot access to admin commands
$this->getCommandsList(); // Load full commands list
foreach ($commands as $command) {
$this->update = new Update(
[
'update_id' => 0,
'message' => [
'message_id' => 0,
'from' => [
'id' => $bot_id,
'first_name' => $bot_name,
'username' => $bot_username
],
'date' => time(),
'chat' => [
'id' => $bot_id,
'type' => 'private',
],
'text' => $command
]
]
);
$this->executeCommand($this->update->getMessage()->getCommand());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment