matriph / telegrambot
此包已被废弃,不再维护。未建议替代包。
PHP 用于 Telegram Bot API 的库。
1.0
2015-06-27 23:00 UTC
Requires
- guzzlehttp/guzzle: ~5.3
This package is auto-updated.
Last update: 2022-10-21 15:51:45 UTC
README
PHP 库用于 Telegram Bot API。
Telegram Bot API
首先您必须有一个 Telegram Bot。要这样做,只需阅读 Telegram 网站的文档。
TL;DR
- 将 @BotFather 添加为开始对话。
- 输入
/newbot
,然后 @BotFather 将询问您的机器人名称。 - 选择一个酷炫的名称,例如
酷炫机器人
并按回车。 - 现在为您的机器人选择一个用户名。它必须以 bot 结尾,例如
酷炫Bot
或酷炫_Bot
。 - 如果成功,@BotFather 将为您提供用于此库的 API 密钥。
安装
最简单的方法是使用 Composer。
composer require matriphe/telegrambot
使用方法
例如,您可以使用库如下所示
<?php require('../vendor/autoload.php'); $apikey = '<fill_your_api_key_here>'; $chat_id = '<user_or_group_id>'; $telegram = new \Matriphe\Telegrambot\Telegrambot($apikey); // Get bot info $getme = $telegram->getMe(); var_dump($getme); // Get bot messages received by bot. See user_id from here. $updates = $telegram->getUpdates(); var_dump($updates); // Send message to user. $message = $telegram->sendMessage([ 'chat_id' => $chat_id, 'text' => 'Today is '.date('Y-m-d H:i:s') ]); var_dump($message); // Upload file, use fopen function. $filepath = '/home/matriphe/photo.jpg'; $photo = $telegram->sendPhoto([ 'chat_id' => $chat_id, 'photo' => fopen($filepath, 'rb'), 'caption' => 'The caption goes here!' ]); var_dump($photo);
所有返回值都在数组中。
函数列表
getMe()
sendMessage()
forwardMessage()
sendPhoto()
sendAudio()
sendDocument()
sendSticker()
sendVideo()
sendLocation()
sendChatAction()
getUserProfilePhotos()
getUpdates()
setWebhook()
请阅读 Telegram Bot API 的方法以获取详细信息。