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.
list methods
SendMessage
CopyMessage
ForwardMessage
SendAudio
SendDocument
SendPhoto
SendVideo
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)();
?>
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')();
}
}
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")();