carlositos / yii2-bot-telegram
bot telegram
Requires
- yiisoft/yii2: *
README
用于web应用yii2
创建你的第一个机器人
-
向@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 carlositos/yii2-bot-telegram "*"
或添加
"carlositos/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
editMessageReplyMarkup
sendGame
Game
Animation
CallbackGame
getGameHighScores
GameHighScore
answerInlineQuery
setChatStickerSet
deleteChatStickerSet
leaveChat
pinChatMessage
unpinChatMessage
setChatDescription
setChatTitle
deleteChatPhoto
exportChatInviteLink
promoteChatMember
restrictChatMember
用法
首先添加到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' ]); ?>