h-soft / telegram-bot
bot telegram
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
Requires (Dev)
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-11-06 02:43:39 UTC
README
H-Soft/Telegram-Bot
Installation
The preferred way to install this extension is through composer.
Either run
composer require h-soft/telegram-bot
or add
"h-soft/telegram-bot": "*"
to the require section of your composer.json
file.
Method list usable
list methods
SendMessage
CopyMessage
ForwardMessage
SendAudio
SendDocument
SendPhoto
SendVideo
Usage
first add to config.php
<?php 'components' => [ 'bot' => [ 'class' => 'h-soft\telegram\Telegram', 'botToken' =>'bot-token', ], ] ?>
Once the extension is installed, simply use it in your code by :
<?php Yii::$app->bot ->SendMessage($chat_id,'TEST')(); ?>
send photo by :
<?php Yii::$app->bot ->sendPhoto($chat_id,Yii::$app->getBaseUrl().'/uploads/test.jpg') ->caption('this is a caption') ->parse_mode('html') ->protect_content(true) ->allow_sending_without_reply(true)(); ?>
Usage in controller
First of all you need to disable the enableCsrfValidation feature in the controller
The robot is currently running from your server But when we start /start run the robot from the telegram application on the mobile, the request does not reach the action inside the controller because the telegram sends the request to the POST and yii requests it without csrf Sends Bad Request (# 400). So then the code doesn't run inside your method
Consider the following example
class SiteController extends Controller { public $enableCsrfValidation = false; public function actionIndex() { $res = Yii::$app->bot->sendMessage($chat_id,'Hello World H-Soft')(); } }
💡 Sample Code:
How to get user chat_id from the bot ?
You can use :
$telegram->input->message->chat->id
to get chat_id
Sample widget class :
$res = Yii::$app->bot->sendMessage($bot->input->message->chat->id,"Hello QalandarDev")();