aki / yii2-bot-telegram
bot telegram
Requires
README
Yii2 bot telegram
创建您的第一个机器人
-
向 @botfather https://telegram.me/botfather 发送以下文本:
/newbot
如果您不知道如何通过用户名发送消息,请点击您的Telegram应用中的搜索字段并输入@botfather
,您应该能够开始对话。请小心不要发送给错误联系人,因为有些用户的用户名与botfather
相似。 -
@botfather 会回复
Alright, a new bot. How are we going to call it? Please choose a name for your bot
. -
为您的机器人输入任何您想要的名称。
-
@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个字符,且必须以 bot 结尾。例如:
PostMan_bot
. -
@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
-
记下上面提到的 'token'。
-
向 @botfather 输入
/setprivacy
。 -
@botfather 会回复
Choose a bot to change group messages settings
. -
输入(或选择)@PostMan_bot(将步骤5上设置的名称更改为您设置的名称,但以 @ 开头)
-
@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
-
输入(或选择)
Disable
以允许您的机器人接收发送到群组的所有消息。实际上这一步取决于您。 -
@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" ]); });