pun2006/yii2-bot-telegram

bot telegram

安装: 4

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 46

类型:yii2-extension

v1.0.0 2024-04-26 08:21 UTC

This package is auto-updated.

Last update: 2024-09-26 11:30:05 UTC


README

Yii2 bot telegram

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads

guide-ru

创建您的第一个机器人

  1. 向 @botfather https://telegram.me/botfather 发送以下文本: /newbot 如果您不知道如何通过用户名发消息,请点击您的 Telegram 应用中的搜索字段并输入 @botfather,您应该能够开始对话。注意不要发送给错误联系人,因为一些用户与 botfather 有类似的用户名。

    createbot

  2. @botfather 回复 Alright, a new bot. How are we going to call it? Please choose a name for your bot

  3. 为您的机器人输入任何想要的名称。

  4. @botfather 回复 Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: PostManGoBot or PostManGo_bot

  5. 为您的机器人输入任何想要的用户名,至少5个字符,且必须以bot结尾。例如: PostMan_bot

  6. @botfather 回复

    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. 向 @botfather 输入 /setprivacy

    capture

  9. @botfather 回复 Choose a bot to change group messages settings

  10. 输入(或选择) @PostMan_bot(将步骤5中设置的名称更改为此,但以@开头)

  11. @botfather 回复。

    '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. @botfather 回复 Success! The new status is: DISABLED. /help

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require aki/yii2-bot-telegram "*"

或添加

"aki/yii2-bot-telegram": "*"

到您的 composer.json 文件的 require 部分。

可用的方法列表

列出方法

getMe
sendMessage
forwardMessage
sendPhoto
sendAudio
sendDocument
sendSticker
sendVideo
sendLocation
sendChatAction
getUserProfilePhotos
getUpdates
setWebhook
getChat
getChatAdministrators
getChatMembersCount
getChatMember
answerCallbackQuery
editMessageText
editMessageCaption
sendGame
Game
Animation
CallbackGame
getGameHighScores
GameHighScore
answerInlineQuery
setChatStickerSet
deleteChatStickerSet
leaveChat
pinChatMessage
unpinChatMessage
setChatDescription
setChatTitle
deleteChatPhoto 
exportChatInviteLink 
promoteChatMember
restrictChatMember
unbanChatMember
kickChatMember
editMessageLiveLocation
stopMessageLiveLocation

用法

首先添加到 config.php

<?php
'components' => [
	'telegram' => [
		'class' => 'aki\telegram\Telegram',
		'botToken' => '112488045:AAGs6CVXgaqC92pvt1u0L6Azfsdfd',
	]
]
?>

扩展安装后,只需在代码中使用它即可

<?php Yii::$app->telegram->sendMessage([
	'chat_id' => $chat_id,
	'text' => 'test',
]); ?>

通过以下方式发送带内联键盘的消息

<?php Yii::$app->telegram->sendMessage([
        'chat_id' => $chat_id,
        'text' => 'this is test',
        'reply_markup' => json_encode([
            'inline_keyboard'=>[
                [
                    ['text'=>"refresh",'callback_data'=> time()]
                ]
            ]
        ]),
    ]); ?>

通过以下方式发送照片

<?php 
Yii::$app->telegram->sendPhoto([
	'chat_id' => $chat_id,
	'photo' => Yii::$app->getBaseUrl().'/uploads/test.jpg',
	'caption' => 'this is test'
]); ?>

在控制器中的用法

首先,您需要在控制器中禁用 enableCsrfValidation 功能

机器人目前运行在您的服务器上。但是,当我们在手机上的 Telegram 应用中启动 /start 命令时,请求没有达到控制器内的操作,因为 Telegram 将请求发送到 POST,而 Yii 则没有 csrf 发送 Bad Request (# 400)。因此,代码没有在您的方法中运行

考虑以下示例

class SiteController extends Controller
{
	public $enableCsrfValidation = false;

	public function actionIndex()
    {
        $res = Yii::$app->telegram->sendMessage([
            'chat_id' => $chat_id,
            'text' => 'hello world!!' 
        ]);
       
    }
}

💡 示例代码

如何从机器人获取用户 chat_id?

您可以使用:$telegram->input->message->chat->id 来获取 chat_id

示例小部件类

$res = Yii::$app->telegram->sendMessage([
	'chat_id' => $telegram->input->message->chat->id,
	'text' => "salam"
]);

💎 新功能 命令

如何使用命令

use aki\telegram\base\Command;

Command::run("/start", function($telegram){
   $result = $telegram->sendMessage([
      'chat_id' => $telegram->input->message->chat->id,
      "text" => "hello"
   ]);
});