shamanhead / telbot
一个用于在Telegram中创建消息机器人的简单库
Requires
- php: >=7.2
This package is not auto-updated.
Last update: 2024-09-28 11:42:54 UTC
README
内容
介绍
如果你想创建你的机器人,首先你需要注册它。你可以通过@BotFather来做到这一点。与它开启对话,并写入/newbot,就像这张图片中显示的那样
之后,你需要在你的机器人上设置webhook。Webhook是一个系统,当Telegram收到查询时,它会向你的服务器发送查询。
在开始创建webhook之前,你需要机器人API令牌和服务器。如果你没有服务器,你可以使用heroku来创建一个。
你可以通过写入/mybots找到你的机器人API令牌,然后选择你的机器人,然后选择“API Token”按钮。示例
完成所有这些操作后,你可以在你的机器人上设置webhook。为此,你需要使用bot api方法setWebhook
然后,如果你做得正确,你会看到这个回答
之后,你可以开始使用你的机器人。但如何操作呢?你可以问我。让我们看看。
创建你的机器人
如何创建机器人?非常简单!只需创建一个新的机器人类
use \Telbot\Bot as Bot; $bot = new Bot('BOT_API_KEY HERE');
所以,让我们创建一个脚本,它将发送一条文本消息作为测试。但如何?Inquiry类将帮助我们做到这一点
use \Telbot\Bot as Bot; use \Telbot\Inquiry as Inquiry; use \Telbot\InputHandle as InputHandle; $bot = new Bot('API_TOKEN'); $InputHandle = new InputHandle(); Inquiry::send($bot ,'sendMessage', [ 'chat_id' => $InputHandle->getChatId(), 'text' => 'Testing your bot.' ] );
实用工具
支持类,便于处理Telegram机器人API类型。
创建键盘
你可以通过这种方式轻松创建键盘
use \Telbot\Utils\ as Utils; use \Telbot\Bot as Bot; Utils::buildInlineKeyboard([[['one', 'text']], [['two', 'text']], [['three', 'test']]]) Utils::buildKeyboard([[['third'], ['second'], ['first']]])
示例
use \Telbot\Utils as Utils; use \Telbot\Bot as Bot; use \Telbot\InputHandle as InputHandle; use \Telbot\Inquiry as Inquiry; $bot = new Bot('API_TOKEN'); $InputHandle = new InputHandle(); Inquiry::send($bot, 'sendMessage', [ 'chat_id' => $InputHandle->getChatId(), 'text' => 'Simple text.', 'reply_markup' => Utils::buildInlineKeyboard([[['one', 'callback']], [['two', 'callback']], [['three', 'callback']]]) ]);
编码文件
如果你想向用户发送视频或照片,你需要将它们编码为CURl格式。为此使用此方法
Utils::encodeFile($filePath) //return encoded CURlfile object.
参数$filePath需要指示你想要发送的文件的路径。
构建内联查询结果
如果你想向内联查询发送答案,你需要构建答案对象。为此使用此方法
Utils::buildInlineQueryResult($resultType ,$data) //returns json encoded array of $data with type of result $resultType
你可以在这里找到示例
MySQL功能
要开始使用MySQL,首先需要在你的机器人对象中启用SQL连接
$bot->enableSql();
你也可以使用类似的方法禁用SQL
$bot->disableSql();
警告:如果你的SQL连接不存在,你不能使用此模块:User、Chat。在这种情况下,将使用文件而不是将上下文值写入数据库来创建新的文件(一个与用户对应)。
稍后你需要指定SQL凭证
$bot->sqlCredentials( [ 'database_server' => '', 'database_name' => '', 'username' => '', 'password' => '' ] );
或者你可以指定你的外部PDO连接作为SQL凭证
$bot->externalPDO($PDO_CONNECTION);
完成所有这些操作后,你可以开始使用数据库。
上下文
使用Context类可以创建上下文依赖
use \Telbot\Context as Context; //We include new class Context use \Telbot\Bot as Bot; use \Telbot\Inquiry as Inquiry; use \Telbot\InputHandle as InputHandle; $InputHandle = new InputHandle(); $bot = new Bot('API_TOKEN'); $DBH = new PDO(); $bot->externalPDO($DBH); $bot->enableSql(); if(!Context::read($bot, $InputHandle->getChatId(), $InputHandle->getUserId())){ //reading context Inquiry::send($bot ,'sendMessage', [ 'chat_id' => $InputHandle->getChatId(), 'text' => 'Write smth' ] ); Context::write($bot, $InputHandle->getChatId(), $InputHandle->getUserId(), 'smth'); //creating new context }else{ Inquiry::send($bot ,'sendMessage', [ 'chat_id' => $InputHandle->getChatId(), 'text' => 'Okay, you writed!' ] ); Context::delete($bot, $InputHandle->getChatId(), $InputHandle->getUserId()); //delete context }
在数据库中处理聊天
与之前相同,但略有不同
要向数据库添加聊天,你需要使用此函数
Chat::add($bot, $chatId);
要删除聊天
Chat::delete($bot, $chatId);
要获取聊天
Chat::get($bot, $chatId);
此函数返回包含此聊天行信息的数组(行ID、聊天ID、机器人令牌)
要获取所有聊天
Chat::getAll($bot);
查询
这是库中的主要类。本段显示了该类的所有功能。
这个类只有一个方法 - Inquiry::send()。使用这个类,您可以发送简单的文本消息和复杂的答案。
Inquiry::send($bot, $method, $data);
支持的方法
API 4.7版本支持的所有方法
发送简单答案
Inquiry::send($bot, 'sendMessage', [ 'chat_id' => $InputHandle->getChatId(), 'text' => 'This is a testing message' ]);
发送回调查询答案
Inquiry::send($bot, 'answerCallbackQuery', [ 'callback_query_id' => $InputHandle->getCallbackQueryId(), 'text' => 'This is a callback answer, who lool like common notification' ]);
发送内联查询答案
Inquiry::answerInlineQuery($bot, [ 'inline_query_id' => $InputHandle->getInlineQueryId(), 'results' => Utils::buildInlineQueryResult('article', [ 'title' => 'test', 'input_message_content' => [ 'message_text' => 'Yes, its just test' ]]) ]);
您可以使用此方法发送任何Telegram方法。所有这些方法都受支持。
发送文件
//sending photo use \Telbot\Bot as Bot; use \Telbot\InputHandle as InputHandle; use \Telbot\Inquiry as Inquiry; $bot = new Bot('927942575:AAHZpZoG2pBRw25Lw-pPaw8FU15t00Lsf3A'); $InputHandle = new InputHandle(); Inquiry::send($bot ,'sendPhoto', [ 'chat_id' => $InputHandle->getChatId(), 'photo' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Telegram_2019_Logo.svg/1200px-Telegram_2019_Logo.svg.png', 'caption' => 'This is an image!So beautiful' ]);
要从您的服务器发送文件,您需要将文件编码为CURl格式。为此,您需要使用方法 Utils::encodeFile。
use \Telbot\Utils as Utils; use \Telbot\Bot as Bot; use \Telbot\InputHandle as InputHandle; use \Telbot\Inquiry as Inquiry; $bot = new Bot('927942575:AAHZpZoG2pBRw25Lw-pPaw8FU15t00Lsf3A'); $InputHandle = new InputHandle(); Inquiry::send($bot ,'sendPhoto', [ 'chat_id' => $InputHandle->getChatId(), 'photo' => Utils::encodeFile('app/something/telegram.png'), 'caption' => 'This is an image!So beautiful' ]);
输入处理
这个类是为了方便处理Telegram答案查询而设计的。
创建新的InputHandle对象
$InputHanle = new InputHandle();
处理数据
$InputHandle->getUpdateId() // returns an update id of telegram answer query. $InputHandle->getQueryType() // returns a query type of telegram answer query(callback_query,inline_query,message). $InputHandle->getInstance() // returns an array of telegram answer. $InputHandle->getCallbackData() // returns a callback data from telegram answer query. $InputHandle->getCallBackQueryId() // returns a callback query id from telegram answer query. $InputHandle->getUserId() // returns user id. $InputHandle->userIsBot() // return true if user who send quiry is bot. $InputHandle->getUserName() // returns name of user, who sends query. $InputHandle->getMessageId() // returns user's message id. $InputHandle->getUserFirstName() // returns user's first name. $InputHandle->getLanguageCode() // returns user's language code. $InputHandle->getChatType() // returns chat type. $InputHandle->getChat() // returns chat array from telegram answer query. $InputHandle->newChatMember() // returns true when new member comes to telegram chat. $InputHandle->getChatId() // returns a chat id, where the message come. $InputHandle->getDate() // returns date when telegram answer query was send. $InputHandle->getEntities() // returns message entities from telegram answer query. $InputHandle->getChatTitle() // returns title of chat where bot gets query. $InputHandle->getInlineQueryText() // returns query data from inline query. $InputHandle->getInlineQueryOffset() // returns query offset from inline query. $InputHandle->getInlineQueryId() // returns an inline query in from telegram answer query.
特权
您可以在聊天中(或所有聊天中)赋予用户特定的特权。这可以用来给特定用户授权某些命令。
Privilege::setToChat($bot, $value, $userId, $chatId); //for one chat
Privilege::setToAllChats($bot, $value, $userId); //for all chats
Privilege::get($bot, $userId); //getting a privelege
示例
请参阅“examples”文件夹中的示例。
许可证
请参阅本存储库中包含的LICENSE文件,以获取MIT许可证的完整副本,本项目受此许可证的许可。