PHP code example of hojjatjh / bot-builder
1. Go to this page and download the library: Download hojjatjh/bot-builder library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hojjatjh / bot-builder example snippets
otBuilder\BotBuilder;
$builder = new BotBuilder(__DIR__ . '/base/telegram-bot');
$builder
->define('BOT_NAME', s/my-new-bot', [
'BOT_NAME' => 'MyCoolBot',
'BOT_TOKEN' => '123456:ABC-DEF',
]);
print_r($files);
return [
'name' => '{{BOT_NAME}}',
'token' => '{{BOT_TOKEN}}',
];
return [
'name' => '{{BOT_NAME}}',
'token' => '{{BOT_TOKEN}}',
];
return [
'secret' => '{{SERVER_KEY}}',
'webhook_url' => '{{WEBHOOK_URL}}',
];
$builder
->define('BOT_NAME', d: true)
->define('SERVER_KEY', '/bots/my-new-bot', [
'BOT_NAME' => 'MyCoolBot',
'BOT_TOKEN' => '123456:ABC-DEF',
'SERVER_KEY' => 's3cr3t',
'WEBHOOK_URL' => 'https://example.com/hook',
]);
$builder
->define('BOT_TOKEN', '0'); // optional, falls back to '0'
$builder->build($target, $values, overwrite: true);
use BotBuilder\Template;
use BotBuilder\BotGenerator;
$generator = new BotGenerator(new Template(__DIR__ . '/base/telegram-bot'));
$files = $generator->generate(__DIR__ . '/bots/my-new-bot', [
'BOT_NAME' => 'MyCoolBot',
]);
use BotBuilder\Template;
use BotBuilder\BotGenerator;
use BotBuilder\PlaceholderReplacer;
$generator = new BotGenerator(
new Template(__DIR__ . '/base/telegram-bot'),
new PlaceholderReplacer('%%', '%%'), // use %%KEY%% instead of {{KEY}}
);
return [
'name' => '{{BOT_NAME}}',
'token' => '{{BOT_TOKEN}}',
'admin_id' => {{ADMIN_ID}},
];
$config = api.telegram.org/bot' . $config['token'] . '/';
$offset = 0;
echo $config['name'] . ' is running...' . PHP_EOL;
while (true) {
$response = file_get_contents($api . 'getUpdates?timeout=30&offset=' . $offset);
$updates = json_decode($response, true);
foreach ($updates['result'] ?? [] as $update) {
$offset = $update['update_id'] + 1;
$chatId = $update['message']['chat']['id'] ?? null;
$text = $update['message']['text'] ?? '';
if ($chatId !== null) {
file_get_contents($api . 'sendMessage?' . http_build_query([
'chat_id' => $chatId,
'text' => 'You said: ' . $text,
]));
}
}
}
use BotBuilder\BotBuilder;
$builder = new BotBuilder(__DIR__ . '/base/telegram-bot');
$builder
->define('BOT_NAME', w-bot', [
'BOT_NAME' => 'MyCoolBot',
'BOT_TOKEN' => '123456:ABC-DEF',
'ADMIN_ID' => '55555',
]);
otBuilder\BotBuilder;
$builder = new BotBuilder(__DIR__ . '/base/telegram-bot');
$builder
->define('BOT_NAME', s/my-new-bot', [
'BOT_NAME' => 'MyCoolBot',
'BOT_TOKEN' => '123456:ABC-DEF',
]);
print_r($files);
return [
'name' => '{{BOT_NAME}}',
'token' => '{{BOT_TOKEN}}',
];
return [
'name' => '{{BOT_NAME}}',
'token' => '{{BOT_TOKEN}}',
];
return [
'secret' => '{{SERVER_KEY}}',
'webhook_url' => '{{WEBHOOK_URL}}',
];
$builder
->define('BOT_NAME', d: true)
->define('SERVER_KEY', '/bots/my-new-bot', [
'BOT_NAME' => 'MyCoolBot',
'BOT_TOKEN' => '123456:ABC-DEF',
'SERVER_KEY' => 's3cr3t',
'WEBHOOK_URL' => 'https://example.com/hook',
]);
$builder
->define('BOT_TOKEN', '0'); // optional, falls back to '0'
$builder->build($target, $values, overwrite: true);
use BotBuilder\Template;
use BotBuilder\BotGenerator;
$generator = new BotGenerator(new Template(__DIR__ . '/base/telegram-bot'));
$files = $generator->generate(__DIR__ . '/bots/my-new-bot', [
'BOT_NAME' => 'MyCoolBot',
]);
use BotBuilder\Template;
use BotBuilder\BotGenerator;
use BotBuilder\PlaceholderReplacer;
$generator = new BotGenerator(
new Template(__DIR__ . '/base/telegram-bot'),
new PlaceholderReplacer('%%', '%%'), // use %%KEY%% instead of {{KEY}}
);
return [
'name' => '{{BOT_NAME}}',
'token' => '{{BOT_TOKEN}}',
'admin_id' => {{ADMIN_ID}},
];
$config = api.telegram.org/bot' . $config['token'] . '/';
$offset = 0;
echo $config['name'] . ' is running...' . PHP_EOL;
while (true) {
$response = file_get_contents($api . 'getUpdates?timeout=30&offset=' . $offset);
$updates = json_decode($response, true);
foreach ($updates['result'] ?? [] as $update) {
$offset = $update['update_id'] + 1;
$chatId = $update['message']['chat']['id'] ?? null;
$text = $update['message']['text'] ?? '';
if ($chatId !== null) {
file_get_contents($api . 'sendMessage?' . http_build_query([
'chat_id' => $chatId,
'text' => 'You said: ' . $text,
]));
}
}
}
use BotBuilder\BotBuilder;
$builder = new BotBuilder(__DIR__ . '/base/telegram-bot');
$builder
->define('BOT_NAME', w-bot', [
'BOT_NAME' => 'MyCoolBot',
'BOT_TOKEN' => '123456:ABC-DEF',
'ADMIN_ID' => '55555',
]);
base/telegram-bot/ -> bots/my-new-bot/
config.php ({{...}}) -> config.php (filled in)
server-key.php ({{...}}) -> server-key.php (filled in)
bot.php -> bot.php
bash
php bots/my-new-bot/bot.php
base/telegram-bot/ -> bots/my-new-bot/
config.php ({{...}}) -> config.php (filled in)
server-key.php ({{...}}) -> server-key.php (filled in)
bot.php -> bot.php
bash
php bots/my-new-bot/bot.php