Skip to content

Commit 4f070fb

Browse files
committedJul 12, 2020
Move all bot configuration to config.php and require it in the individual files.
1 parent 8b6ef86 commit 4f070fb

9 files changed

+196
-292
lines changed
 

Diff for: ‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
composer.phar
2-
/vendor/
32
composer.lock
3+
config.php
4+
vendor

Diff for: ‎README.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# PHP Telegram Bot Example
2-
An A-Z example of Telegram bot using the [PHP Telegram Bot][1] library.
2+
An A-Z example of Telegram bot using the [PHP Telegram Bot][core-github] library.
33

44
This repository aims to demonstrate the usage of all the features offered by the PHP Telegram Bot library and as such contains all example commands.
5-
Also, it gives an example setup for both the standard usage and using the [PHP Telegram Bot Manager][3]
5+
Also, it gives an example setup for both the standard usage and using the [PHP Telegram Bot Manager][bot-manager-github]
66

77
**:exclamation: Important!**
88
- Most of the commands found here are **not to be used exactly as they are**, they are mere demonstrations of features! They are provided as-is and any **extra security measures need to be added by you**, the developer.
9-
- Before getting started with this project, make sure you have read the official [readme][2] to understand how the PHP Telegram Bot library works and what is required to run a Telegram bot.
9+
- Before getting started with this project, make sure you have read the official [readme][core-readme-github] to understand how the PHP Telegram Bot library works and what is required to run a Telegram bot.
1010

1111
Let's get started then! :smiley:
1212

@@ -28,14 +28,16 @@ Unzip the files to the root of your project folder.
2828

2929
## 1. Making it yours
3030

31-
Now you can choose what installation you would like, either the default one or using the [Bot Manager][3] project.
31+
Now you can choose what installation you would like, either the default one or using the [Bot Manager][bot-manager-github] project.
3232
Depending on which one you choose, you can delete the files that are not required.
3333

3434
---
3535

36+
First of all, you need to rename `config.example.php` to `config.php` and then replace all necessary values with those of your project.
37+
3638
**Default**
37-
Next, edit the following files, replacing all necessary values with those of your project.
38-
Thanks to reading the main readme file, you should know what these do.
39+
Some of these files require extra configurations to be added. Check `hook.php` how they are loaded.
40+
Thanks to reading the main readme file, you should know what these files do.
3941

4042
- `composer.json` (Describes your project and it's dependencies)
4143
- `set.php` (Used to set the webhook)
@@ -47,7 +49,7 @@ Thanks to reading the main readme file, you should know what these do.
4749
**Bot Manager**
4850
Using the bot manager makes life much easier, as all configuration goes into a single file, `manager.php`.
4951

50-
If you decide to use the Bot Manager, be sure to [read all about it][4] and change the `require` block in the `composer.json` file:
52+
If you decide to use the Bot Manager, be sure to [read all about it][bot-manager-readme-github] and change the `require` block in the `composer.json` file:
5153
```json
5254
"require": {
5355
"php-telegram-bot/telegram-bot-manager": "*"
@@ -61,15 +63,15 @@ Then, edit the following files, replacing all necessary values with those of you
6163

6264
---
6365

64-
Now you can install all dependencies using [composer][5]:
66+
Now you can install all dependencies using [composer]:
6567
```bash
6668
$ composer install
6769
```
6870

6971
## To be continued!
7072

71-
[1]: https://github.com/php-telegram-bot/core "php-telegram-bot/core"
72-
[2]: https://github.com/php-telegram-bot/core#readme "PHP Telegram Bot - README"
73-
[3]: https://github.com/php-telegram-bot/telegram-bot-manager "php-telegram-bot/telegram-bot-manager"
74-
[4]: https://github.com/php-telegram-bot/telegram-bot-manager#readme "PHP Telegram Bot Manager - README"
75-
[5]: https://getcomposer.org/ "Composer"
73+
[core-github]: https://github.com/php-telegram-bot/core "php-telegram-bot/core"
74+
[core-readme-github]: https://github.com/php-telegram-bot/core#readme "PHP Telegram Bot - README"
75+
[bot-manager-github]: https://github.com/php-telegram-bot/telegram-bot-manager "php-telegram-bot/telegram-bot-manager"
76+
[bot-manager-readme-github]: https://github.com/php-telegram-bot/telegram-bot-manager#readme "PHP Telegram Bot Manager - README"
77+
[composer]: https://getcomposer.org/ "Composer"

Diff for: ‎config.example.php

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHP Telegram Bot example-bot package.
5+
* https://github.com/php-telegram-bot/example-bot/
6+
*
7+
* (c) PHP Telegram Bot Team
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
/**
14+
* This file contains all the configuration options for the PHP Telegram Bot.
15+
*
16+
* It is based on the configuration array of the PHP Telegram Bot Manager project.
17+
*
18+
* Simply adjust all the values that you need and extend where necessary.
19+
*
20+
* Options marked as [Manager Only] are only required if you use `manager.php`.
21+
*
22+
* For a full list of all options, check the Manager Readme:
23+
* https://github.com/php-telegram-bot/telegram-bot-manager#set-extra-bot-parameters
24+
*/
25+
26+
return [
27+
// Add you bot's API key and name
28+
'api_key' => 'your:bot_api_key',
29+
'bot_username' => 'username_bot', // Without "@"
30+
31+
// [Manager Only] Secret key required to access the webhook
32+
'secret' => 'super_secret',
33+
34+
// When using the getUpdates method, this can be commented out
35+
'webhook' => [
36+
'url' => 'https://your-domain/path/to/hook-or-manager.php',
37+
// Use self-signed certificate
38+
// 'certificate' => __DIR__ . '/path/to/your/certificate.crt',
39+
// Limit maximum number of connections
40+
// 'max_connections' => 5,
41+
],
42+
43+
// All command related configs go here
44+
'commands' => [
45+
// Define all paths for your custom commands
46+
'paths' => [
47+
// __DIR__ . '/Commands',
48+
],
49+
// Here you can set any command-specific parameters
50+
'configs' => [
51+
// - Google geocode/timezone API key for /date command
52+
// 'date' => ['google_api_key' => 'your_google_api_key_here'],
53+
// - Payment Provider Token for /payment command.
54+
// 'payment' => ['payment_provider_token' => 'your_payment_provider_token_here'],
55+
],
56+
],
57+
58+
// Define all IDs of admin users
59+
'admins' => [
60+
// 123,
61+
],
62+
63+
// Enter your MySQL database credentials
64+
// 'mysql' => [
65+
// 'host' => '127.0.0.1',
66+
// 'user' => 'root',
67+
// 'password' => 'root',
68+
// 'database' => 'telegram_bot',
69+
// ],
70+
71+
// Logging (Debug, Error and Raw Updates)
72+
// 'logging' => [
73+
// 'debug' => __DIR__ . '/php-telegram-bot-debug.log',
74+
// 'error' => __DIR__ . '/php-telegram-bot-error.log',
75+
// 'update' => __DIR__ . '/php-telegram-bot-update.log',
76+
// ],
77+
78+
// Set custom Upload and Download paths
79+
'paths' => [
80+
'download' => __DIR__ . '/Download',
81+
'upload' => __DIR__ . '/Upload',
82+
],
83+
84+
// Requests Limiter (tries to prevent reaching Telegram API limits)
85+
'limiter' => [
86+
'enabled' => true,
87+
],
88+
];

Diff for: ‎cron.php

+14-64
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
*/
1212

1313
/**
14-
* README
15-
* This configuration file is intended to run a list of commands with crontab.
16-
* Uncommented parameters must be filled
14+
* This file is used to run a list of commands with crontab.
1715
*/
1816

19-
// Your command(s) to run, pass it just like in a message (arguments supported)
17+
// Your command(s) to run, pass them just like in a message (arguments supported)
2018
$commands = [
2119
'/whoami',
2220
"/echo I'm a bot!",
@@ -25,76 +23,28 @@
2523
// Load composer
2624
require_once __DIR__ . '/vendor/autoload.php';
2725

28-
// Add you bot's API key and name
29-
$bot_api_key = 'your:bot_api_key';
30-
$bot_username = 'username_bot';
31-
32-
// Define all IDs of admin users in this array (leave as empty array if not used)
33-
$admin_users = [
34-
// 123,
35-
];
36-
37-
// Define all paths for your custom commands in this array (leave as empty array if not used)
38-
$commands_paths = [
39-
// __DIR__ . '/Commands/',
40-
];
41-
42-
// Enter your MySQL database credentials
43-
//$mysql_credentials = [
44-
// 'host' => 'localhost',
45-
// 'user' => 'dbuser',
46-
// 'password' => 'dbpass',
47-
// 'database' => 'dbname',
48-
//];
26+
// Load all configuration options
27+
/** @var array $config */
28+
$config = require __DIR__ . '/config.php';
4929

5030
try {
5131
// Create Telegram API object
52-
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
53-
54-
// Add commands paths containing your custom commands
55-
$telegram->addCommandsPaths($commands_paths);
32+
$telegram = new Longman\TelegramBot\Telegram($config['api_key'], $config['bot_username']);
5633

57-
// Enable admin users
58-
$telegram->enableAdmins($admin_users);
59-
60-
// Enable MySQL
61-
//$telegram->enableMySql($mysql_credentials);
62-
63-
// Logging (Error, Debug and Raw Updates)
64-
// https://github.com/php-telegram-bot/core/blob/master/doc/01-utils.md#logging
65-
//
66-
// (this example requires Monolog: composer require monolog/monolog)
67-
//Longman\TelegramBot\TelegramLog::initialize(
68-
// new Monolog\Logger('telegram_bot', [
69-
// (new Monolog\Handler\StreamHandler(__DIR__ . "/{$bot_username}_debug.log", Monolog\Logger::DEBUG))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
70-
// (new Monolog\Handler\StreamHandler(__DIR__ . "/{$bot_username}_error.log", Monolog\Logger::ERROR))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
71-
// ]),
72-
// new Monolog\Logger('telegram_bot_updates', [
73-
// (new Monolog\Handler\StreamHandler(__DIR__ . "/{$bot_username}_update.log", Monolog\Logger::INFO))->setFormatter(new Monolog\Formatter\LineFormatter('%message%' . PHP_EOL)),
74-
// ])
75-
//);
76-
77-
// Set custom Upload and Download paths
78-
//$telegram->setDownloadPath(__DIR__ . '/Download');
79-
//$telegram->setUploadPath(__DIR__ . '/Upload');
80-
81-
// Here you can set some command specific parameters,
82-
// e.g. Google geocode/timezone api key for /date command:
83-
//$telegram->setCommandConfig('date', ['google_api_key' => 'your_google_api_key_here']);
84-
85-
// Requests Limiter (tries to prevent reaching Telegram API limits)
86-
$telegram->enableLimiter();
34+
/**
35+
* Check `hook.php` for configuration code to be added here.
36+
*/
8737

8838
// Run user selected commands
8939
$telegram->runCommands($commands);
9040

9141
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
92-
// Silence is golden!
93-
//echo $e;
9442
// Log telegram errors
9543
Longman\TelegramBot\TelegramLog::error($e);
44+
45+
// Uncomment this to output any errors (ONLY FOR DEVELOPMENT!)
46+
// echo $e;
9647
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
97-
// Silence is golden!
98-
// Uncomment this to catch log initialisation errors
99-
//echo $e;
48+
// Uncomment this to output log initialisation errors (ONLY FOR DEVELOPMENT!)
49+
// echo $e;
10050
}

Diff for: ‎getUpdatesCLI.php

+16-66
Original file line numberDiff line numberDiff line change
@@ -12,92 +12,42 @@
1212
*/
1313

1414
/**
15-
* README
16-
* This configuration file is intended to run the bot with the getUpdates method.
17-
* Uncommented parameters must be filled
18-
*
19-
* Bash script:
20-
* $ while true; do ./getUpdatesCLI.php; done
15+
* This file is used to run the bot with the getUpdates method.
2116
*/
2217

2318
// Load composer
2419
require_once __DIR__ . '/vendor/autoload.php';
2520

26-
// Add you bot's API key and name
27-
$bot_api_key = 'your:bot_api_key';
28-
$bot_username = 'username_bot';
29-
30-
// Define all IDs of admin users in this array (leave as empty array if not used)
31-
$admin_users = [
32-
// 123,
33-
];
34-
35-
// Define all paths for your custom commands in this array (leave as empty array if not used)
36-
$commands_paths = [
37-
__DIR__ . '/Commands/',
38-
];
39-
40-
// Enter your MySQL database credentials
41-
$mysql_credentials = [
42-
'host' => 'localhost',
43-
'user' => 'dbuser',
44-
'password' => 'dbpass',
45-
'database' => 'dbname',
46-
];
21+
// Load all configuration options
22+
/** @var array $config */
23+
$config = require __DIR__ . '/config.php';
4724

4825
try {
4926
// Create Telegram API object
50-
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
51-
52-
// Add commands paths containing your custom commands
53-
$telegram->addCommandsPaths($commands_paths);
54-
55-
// Enable admin users
56-
$telegram->enableAdmins($admin_users);
27+
$telegram = new Longman\TelegramBot\Telegram($config['api_key'], $config['bot_username']);
5728

58-
// Enable MySQL
59-
$telegram->enableMySql($mysql_credentials);
60-
61-
// Logging (Error, Debug and Raw Updates)
62-
// https://github.com/php-telegram-bot/core/blob/master/doc/01-utils.md#logging
63-
//
64-
// (this example requires Monolog: composer require monolog/monolog)
65-
//Longman\TelegramBot\TelegramLog::initialize(
66-
// new Monolog\Logger('telegram_bot', [
67-
// (new Monolog\Handler\StreamHandler(__DIR__ . "/{$bot_username}_debug.log", Monolog\Logger::DEBUG))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
68-
// (new Monolog\Handler\StreamHandler(__DIR__ . "/{$bot_username}_error.log", Monolog\Logger::ERROR))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
69-
// ]),
70-
// new Monolog\Logger('telegram_bot_updates', [
71-
// (new Monolog\Handler\StreamHandler(__DIR__ . "/{$bot_username}_update.log", Monolog\Logger::INFO))->setFormatter(new Monolog\Formatter\LineFormatter('%message%' . PHP_EOL)),
72-
// ])
73-
//);
74-
75-
// Set custom Upload and Download paths
76-
//$telegram->setDownloadPath(__DIR__ . '/Download');
77-
//$telegram->setUploadPath(__DIR__ . '/Upload');
78-
79-
// Here you can set some command specific parameters
80-
// e.g. Google geocode/timezone api key for /date command
81-
//$telegram->setCommandConfig('date', ['google_api_key' => 'your_google_api_key_here']);
82-
83-
// Requests Limiter (tries to prevent reaching Telegram API limits)
84-
$telegram->enableLimiter();
29+
/**
30+
* Check `hook.php` for configuration code to be added here.
31+
*/
8532

8633
// Handle telegram getUpdates request
8734
$server_response = $telegram->handleGetUpdates();
8835

8936
if ($server_response->isOk()) {
9037
$update_count = count($server_response->getResult());
91-
echo date('Y-m-d H:i:s', time()) . ' - Processed ' . $update_count . ' updates';
38+
echo date('Y-m-d H:i:s') . ' - Processed ' . $update_count . ' updates';
9239
} else {
93-
echo date('Y-m-d H:i:s', time()) . ' - Failed to fetch updates' . PHP_EOL;
40+
echo date('Y-m-d H:i:s') . ' - Failed to fetch updates' . PHP_EOL;
9441
echo $server_response->printError();
9542
}
43+
9644
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
97-
echo $e->getMessage();
9845
// Log telegram errors
9946
Longman\TelegramBot\TelegramLog::error($e);
47+
48+
// Uncomment this to output any errors (ONLY FOR DEVELOPMENT!)
49+
// echo $e;
10050
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
101-
// Catch log initialisation errors
102-
echo $e->getMessage();
51+
// Uncomment this to output log initialisation errors (ONLY FOR DEVELOPMENT!)
52+
// echo $e;
10353
}

0 commit comments

Comments
 (0)