اسکریپت باران
بزرگترین مرجع دانلود سورس و اسکریپت های برنامه نویسی
در این پست از سایت اسکریپت باران قصد داریم اسکریپت خالص ربات ساز تلگرام را قرار دهیم که بطور کامل توسط پلاگین هایی توسعه یافته است. تلگرام به تازگی اعلام کرده که از API های روبات بطور رسمی پشتیبانی می کند.
برخی امکانات :
تغییرات نسخه جدید :
Add request limiter
Small fix in logger
آموزش ایجاد اولین ربات :
1 . به آیدی https://telegram.me/botfather در تلگرام این پیام را ارسال کنید : newbot/
اگر نمیدانید چطور به وسیله آیدی، به شخصی پیام ارسال کنید، در برنامه تلگرام بر روی سرچ فیلد کلیک کنید و تایپ کنید botfather@ . حالا می توانید به روبات پیام ارسال کنید. مراقب باشید که به اکانت های مشابه این یوزرنیم در لیست سرچ پیام ارسال نکنید.
2. بوت فادر پاسخ می دهد : Alright, a new bot. How are we going to call it? Please choose a name for your bot.
3. هر نامی را که میخواهید، برای روبات خود تایپ کنید.
4. بوت فادر پاسخ می دهد : Good. Now let’s choose a username for your bot. It must end in bot. Like this, for example: TetrisBot or tetris_bot.
5. هر یوزر نیمی که برای روبات خود مد نظر دارید را تایپ کنید. نام باید حداقل 5 کاراکتر باشد و در انتهای نامش به کلمه bot ختم شود، مثلا : scriptbaran_bot
6. بوت فادر پاسخ می دهد :
1 2 3 4 5 6 7 8 9 10 |
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: https://core.telegram.org/bots/api |
7. کد token دریافتی در انتهای پیام را در جایی یادداشت و کپی کنید.
8. این پیام را به بوت فادر ارسال کنید : setprivacy/
9 بوت فادر پاسخ می دهد : Choose a bot to change group messages settings.
10. نام یوزنیم ربات خود را که در مراحل قبل ایجاد کردید همراه با @ در ابتدای آن، تایپ کنید.
11. بوت فادر پاسخ می دهد :
1 2 3 |
'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username. 'Disable' - your bot will receive all messages that people send to groups. Current status is: ENABLED |
12. کلمه disable را تایپ کنید تا ربات شما همه پیام هایی که در گروه ارسال می شود را دریافت کند.
13. بوت فادر پاسخ می دهد : Success! The new status is: DISABLED. /help
نصب پکیج با استفاده از Composer :
فایل ستاپ composer برای ویندوز را از این آدرس دانلود کنید : https://getcomposer.org/download/
فایل composer.json پروژه خود را در ویرایش کرده تا این مسیر را حتما لود کند : longman/telegram-bot مانند مثال زیر :
1 2 3 4 5 6 7 8 |
{{ "name": "yourproject/yourproject", "type": "project", "require": { "php": ">=5.5.0", "longman/telegram-bot": "*" } } |
و این دستور را اجرا کنید : composer update
یا این دستور را در command line خود اجرا کنید :
1 2 |
composer require longman/telegram-bot |
انتخاب چگونگی دریافت آپدیت های تلگرام :
این ربات می تواند آپدیت های تلگرام را با استفاده از دو متد webhook یا getUpdate دریافت کند:
Webhook | getUpdate | |
---|---|---|
توضیحات | تلگرام اطلاعات را مستقیما به هاست شما ارسال می کند | شما باید آپدیت های تلگرام را بصورت دستی دریافت کنید |
Host with https | الزامی | غیر الزامی |
MySQL | غیر الزامی | الزامی |
نصب Webhook :
برای استفاده از متد webhook شما به سروری اختیاج دارید که https و composer را پشتیبانی کند. (برای امنیت بیشتر نیاز هست تا کد های بیشتری قرار دهید)
یک فایل به نام set.php بسازید (یا فقط کد زیر را در مسیر examples/set.php کپی کنید) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php // Load composer require __DIR__ . '/vendor/autoload.php'; $API_KEY = 'your_bot_api_key'; $BOT_NAME = 'namebot'; $hook_url = 'https://yourdomain/path/to/hook.php'; try { // Create Telegram API object $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME); // Set webhook $result = $telegram->setWebhook($hook_url); if ($result->isOk()) { echo $result->getDescription(); } } catch (Longman\TelegramBot\Exception\TelegramException $e) { echo $e; } |
فایل set.php را با مرورگر خود باز کنید تا webhook را با تلگرام همگام سازی کنید.
حالا یک فایل به نام hook.php بسازید (یا فقط کد زیر را در مسیر examples/hook.php کپی کنید) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php // Load composer require __DIR__ . '/vendor/autoload.php'; $API_KEY = 'your_bot_api_key'; $BOT_NAME = 'namebot'; try { // Create Telegram API object $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME); // Handle telegram webhook request $telegram->handle(); } catch (Longman\TelegramBot\Exception\TelegramException $e) { // Silence is golden! // log telegram errors // echo $e; } |
ایجاد Certificate :
برای آپلود فایل certificate، پارامتر certificate path را در فایل set.php قرار دهید:
1 |
$result = $telegram->setWebhook($hook_url, ['certificate' => $certificate_path]); |
غیرفعالسازی Webhook :
فایل example/unset.php را با اطلاعات ربات خود ویرایش کرده و فایل را اجرا کنید.
نصب getUpdate :
برای نصب، دیتابیس MYSQL باید حتما فعال باشد!
حالا یک فایل به نام getUpdateCLI.php بسازید (یا فقط کد زیر را در مسیر examples/getUpdateCLI.php کپی کنید) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#!/usr/bin/env php <?php require __DIR__ . '/vendor/autoload.php'; $API_KEY = 'your_bot_api_key'; $BOT_NAME = 'namebot'; $mysql_credentials = [ 'host' => 'localhost', 'user' => 'dbuser', 'password' => 'dbpass', 'database' => 'dbname', ]; try { // Create Telegram API object $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME); // Enable MySQL $telegram->enableMySQL($mysql_credentials); // Handle telegram getUpdate request $telegram->handleGetUpdates(); } catch (Longman\TelegramBot\Exception\TelegramException $e) { // log telegram errors echo $e; } |
پرمیشن های فایل را با دستور زیر تعیین کنید :
1 2 |
chmod 775 getUpdateCLI.php |
سپس دستور زیر را اجرا کتید :
1 2 |
./getUpdateCLI.php |
پشتیبانی از :
All types are implemented according to Telegram API (20 January 2016).
Full support for inline query according to Telegram API (20 January 2016).
All methods are implemented according to Telegram API (20 January 2016).
Messages longer than 4096 characters are split up into multiple messages.
1 |
$result = Request::sendMessage(['chat_id' => $chat_id, 'text' => 'Your utf8 text 😜 ...']); |
To send a local photo, provide the file path as the second parameter:
1 2 |
$data = ['chat_id' => $chat_id]; $result = Request::sendPhoto($data, $telegram->getUploadPath() . '/image.jpg'); |
If you know the file_id
of a previously uploaded file, just include it in the data array:
1 2 3 4 5 |
$data = [ 'chat_id' => $chat_id, 'photo' => $file_id, ]; $result = Request::sendPhoto($data); |
sendAudio, sendDocument, sendSticker, sendVideo and sendVoice all work in the same way. See examples/Commands/ImageCommand.php for a full example.
1 |
Request::sendChatAction(['chat_id' => $chat_id, 'action' => 'typing']); |
Retrieve the user photo, see src/Commands/WhoamiCommand.php for a full example.
Get the file path and download it, see src/Commands/WhoamiCommand.php for a full example.
To do this you have to enable the MySQL connection. Here’s an example of use:
1 2 3 4 5 6 7 8 9 |
$results = Request::sendToActiveChats( 'sendMessage', // callback function to execute (see Request.php for available methods) ['text' => 'Hey! Check out the new features!!'], // Data to pass to the request true, // Send to chats (group chat) true, // Send to chats (super group chat) true, // Send to users (single chat) null, // 'yyyy-mm-dd hh:mm:ss' date range from null, // 'yyyy-mm-dd hh:mm:ss' date range to ); |
You can also broadcast a message to users, from the private chat with your bot. Take a look at the admin commands below.
If you want to save messages/users/chats for further usage in commands, create a new database, import structure.sql and enable MySQL support after object creation and BEFORE handle method:
1 2 3 4 5 6 7 8 |
$mysql_credentials = [ 'host' => 'localhost', 'user' => 'dbuser', 'password' => 'dbpass', 'database' => 'dbname', ]; $telegram->enableMySQL($mysql_credentials); |
You can set a custom prefix to all the tables while you are enabling MySQL:
1 |
$telegram->enableMySQL($mysql_credentials, $BOT_NAME . '_'); |
Consider to use the utf8mb4 branch if you find some special characters problems. You can also store inline query and chosen inline query in the database.
Is possible to provide to the library an external mysql connection. Here’s how to configure it:
1 2 |
$telegram->enableExternalMysql($external_pdo_connection) //$telegram->enableExternalMySQL($external_pdo_connection, $table_prefix) |
All methods implemented can be used to manage channels. With admin commands you can manage your channels directly with your bot private chat.
You can enable the integration using this line:
1 |
$telegram->enableBotan('your_token'); |
Replace 'your_token'
with your Botan.io token, check this page to see how to obtain one.
The following actions will be tracked:
Command (/command_name)
in the statsIn order to use the URL shortener you must include the class use Longman\TelegramBot\Botan;
and call it like this:
Botan::shortenUrl('https://github.com/akalongman/php-telegram-bot', $user_id);
Shortened URLs are cached in the database (if MySQL storage is enabled).
The bot is able to recognise commands in a chat with multiple bots (/command@mybot). It can execute command triggering a chat event. Here’s the list:
GenericCommand.php lets you handle commands that don’t exist or to use commands as a variable:
Favourite colour? /black, /red
Favourite number? /1, /134
GenericmessageCommand.php lets you handle any type of message.
Maybe you would like to develop your own commands. A good practice is to store them outside vendor/. This can be done using:
1 2 |
$commands_folder = __DIR__ . '/Commands/'; $telegram->addCommandsPath($commands_folder); |
Inside examples/Commands/ there are some samples that show how to use types.
With this method you can set some command specific parameters:
1 2 3 4 5 |
//Google geocode/timezone API key for date command $telegram->setCommandConfig('date', ['google_api_key' => 'your_google_api_key_here']); //OpenWeatherMap API key for weather command $telegram->setCommandConfig('weather', ['owm_api_key' => 'your_owm_api_key_here']); |
Enabling this feature, the admin bot can perform some super user commands like:
You can specify one or more admins with this option:
1 2 3 4 5 |
//Single admin $telegram->enableAdmin(your_telegram_user_id); //Multiple admins $telegram->enableAdmins([your_telegram_user_id, other_telegram_user_id]); |
Telegram user id can be retrieved with the command /whoami. Admin commands are stored in src/Admin/ folder. To get a list of all available commands, type /help.
To enable this feature follow these steps:
1 |
$telegram->setCommandConfig('sendtochannel', ['your_channel' => ['@type_here_your_channel']]); |
1 |
$telegram->setCommandConfig('sendtochannel', ['your_channel'=>['@type_here_your_channel', '@type_here_another_channel', '@and_so_on']]); |
You can override the default Upload and Download directory with:
1 2 |
$telegram->setDownloadPath('yourpath/Download'); $telegram->setUploadPath('yourpath/Upload'); |
در صورتی که درخواست کاربران زیاد باشد، آموزش ساخت و راه اندازی آن را بصورت اختصاصی و کامل در اسکریپت باران قرار می دهیم. کامنت های شما می تواند باعث دلگرمی ما در ادامه این آموزش شود.
توجه : این پست برای درج آموزش های جدیدتر، هرچند وقت آپدیت می شود.
لینک های دانلود
کانال تلگرامی اسکریپت باران
برای اطلاع از آپدیت های این سورس و سورس های دیگر اسکریپت باران، در کانال تلگرام ما عضو شویدبرنامه نویسی را معنایی دوباره بخشیم !