firedemonsu/yii2-bot-telegram

bot telegram

安装: 100

依赖: 0

建议者: 0

安全: 0

星级: 0

关注者: 0

分支: 46

类型:yii2-extension

2.0.5 2020-06-01 12:43 UTC

This package is auto-updated.

Last update: 2024-09-29 05:36:21 UTC


README

Yii2 bot telegram

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads

创建你的第一个机器人

  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' => 'path/to/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!!' 
        ]);
       return $this->render('index');
    }
}

💡 示例代码

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

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

示例小部件类

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