/telegram-bot

2022-yil “H-Soft” tomonidan rejalashtirilgan loyihalardan biri H-Soft/Telegram-Bot

Primary LanguagePHPBSD 2-Clause "Simplified" LicenseBSD-2-Clause

H-Soft/Telegram-Bot

Latest Stable Version Latest Unstable Version Total Downloads Daily Downloads

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")();