Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TelegramBot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
TelegramBot
Commits
4e511e9d
Commit
4e511e9d
authored
Jan 09, 2017
by
Armando Lüscher
Committed by
GitHub
Jan 09, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #382 from jacklul/crontab
Crontab-friendly script
parents
284b1e95
63a948dd
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
127 additions
and
0 deletions
+127
-0
cron.php
examples/cron.php
+74
-0
Telegram.php
src/Telegram.php
+53
-0
No files found.
examples/cron.php
0 → 100644
View file @
4e511e9d
<?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;
}
src/Telegram.php
View file @
4e511e9d
...
...
@@ -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
());
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment