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
3ae7a0ef
Commit
3ae7a0ef
authored
Jan 01, 2017
by
Jack'lul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add an easy way to run commands from crontab / scripts / webspace!
parent
b12b2e99
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
142 additions
and
0 deletions
+142
-0
cron.php
examples/cron.php
+78
-0
Telegram.php
src/Telegram.php
+64
-0
No files found.
examples/cron.php
0 → 100644
View file @
3ae7a0ef
<?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, use associative array to pass arguments
$commands
=
[
'whoami'
,
'echo'
];
//$commands = ['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
);
// Run user selected commands and modify update array
/*$telegram->runCommands($commands, ['message' => ['text' => 'Parameter']]);*/
}
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 @
3ae7a0ef
...
...
@@ -830,4 +830,68 @@ class Telegram
return
$this
;
}
/**
* Run provided commands
*
* @param array $commands
* @param array $update
*
* @throws TelegramException
*/
public
function
runCommands
(
$commands
,
array
$update
=
[])
{
if
(
!
isset
(
$commands
)
||
!
is_array
(
$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
)
{
throw
new
TelegramException
(
'Received invalid getMe result!'
);
}
$bot_id
=
$result
->
getId
();
$bot_name
=
$result
->
getFirstName
();
$update_template
=
[
'update_id'
=>
0
,
'message'
=>
[
'message_id'
=>
0
,
'from'
=>
[
'id'
=>
$bot_id
,
'first_name'
=>
$bot_name
],
'date'
=>
time
(),
'chat'
=>
[
'id'
=>
$bot_id
,
'type'
=>
'private'
,
],
'text'
=>
''
]
];
$update
=
array_merge
(
$update_template
,
$update
);
$this
->
enableAdmin
(
$bot_id
);
// Give bot access to admin commands
$this
->
getCommandsList
();
// Load full commands list
foreach
(
$commands
as
$command
=>
$parameter
)
{
if
(
is_numeric
(
$command
))
{
// if array/key is not associative $command will be integer and $parameter will be the actual command!
$command
=
$parameter
;
}
$temp_update
=
$update
;
if
(
$parameter
&&
$parameter
!=
$command
)
{
$temp_update
[
'message'
][
'text'
]
=
$parameter
;
}
$this
->
update
=
new
Update
(
$temp_update
);
// this prevents commands throwing exceptions about missing Update objects
$this
->
executeCommand
(
$command
);
}
}
}
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