Telegram BOT API (Yii2)

Создание / Регистрация бота

@BotFather

Описание реализации на mirocow/yii2-telegram-api

Пример реализации на mirocow/yii2-telegram-api

Настройка

'modules' => [ 'telegram' => [ 'class' => 'mirocow\telegram\Module', 'token' => 'api-telegram-token', 'commands' => [ 'help' => 'app\commands\HelpCommand', ], ], ],

Запуск бота

$ php ./yii mirocow\telegram\bot

Создание команд бота

nano app\commands\HelpCommand

Отправка сообщений

Описание реализации на akalongman/php-telegram-bot

Пример реализации на akalongman/php-telegram-bot

Webhook

Простой пример приветствия завязанного на Webhook, реализованного на Yii2 с помощью. Обязательное наличиче SSL. И хук должен отправляться на https:

class APIController { public $API_KEY = 'your_bot_api_key'; public $BOT_NAME = 'namebot'; public $API_CERTIFICATE = ''; public function init(){ try { $telegram = new Longman\TelegramBot\Telegram($this->API_KEY, $this->BOT_NAME); echo $telegram->setWebHook('https://yourdomain/api/init', $this->API_CERTIFICATE); } catch (Longman\TelegramBot\Exception\TelegramException $e) { echo $e->getMessage(); } } public function actionInit(){ try { $telegram = new Longman\TelegramBot\Telegram($this->API_KEY, $this->BOT_NAME); $telegram->addCommandsPath(Yii::getAlias('@app\commands')); $telegram->handle(); } catch (Longman\TelegramBot\Exception\TelegramException $e) { // echo $e->getMessage(); } } }

SSL for WebHook

http://www.startssl.com/ (free for noncommerce)

https://buy.wosign.com/free/?lan=en

getUpdates

Другой метод не требует наличия SSL - это getUpdates

class APIController { public $API_KEY = 'your_bot_api_key'; public $BOT_NAME = 'namebot'; public $COMMANDS_FOLDER = 'app/commands'; public function init(){ try { $telegram = new Longman\TelegramBot\Telegram($this->API_KEY, $this->BOT_NAME); $telegram->enableMySQL($credentials); $telegram->addCommandsPath($this->COMMANDS_FOLDER); $telegram->handleGetUpdates(); } catch (Longman\TelegramBot\Exception\TelegramException $e) { echo $e->getMessage(); } } }

Исходники

Yii2

https://github.com/Mirocow/yii2-telegram-api

https://github.com/SonkoDmitry/yii2-telegram-bot-api

PHP

https://github.com/TelegramBot/Api

https://github.com/akalongman/php-telegram-bot

https://github.com/Ardakilic/Telegram-bot-php

https://github.com/Surzhikov/TelegramSiteHelper

https://github.com/zelenin/telegram-bot-api

https://github.com/mgp25/Telegram-Bot-API

https://github.com/NeonXP/TelegramApi

https://github.com/pathetic/tgbot-php

https://github.com/paranoiasystem/TelegramBot-PHP-library

https://github.com/matriphe/telegrambot

https://github.com/smoqadam/php-telegram-bot

https://github.com/Surzhikov/TelegramSiteHelper

https://github.com/irazasyed/telegram-bot-sdk

Examples / Примеры

https://core.telegram.org/bots/samples/hellobot

https://github.com/kolar/telegram-poll-bot (Longpoll)

API

https://core.telegram.org/bots/api

Статьи

http://habrahabr.ru/post/264035/

http://olegorestov.ru/this/bitrix_php_telegram_bot/

https://github.com/Surzhikov/TelegramSiteHelper/wiki/%D0%9A%D0%90%D0%9A-%D0%A3%D0%A1%D0%A2%D0%90%D0%9D%D0%9E%D0%92%D0%98%D0%A2%D0%AC