PHP集成Telegram Bot 机器人发送消息-详细流程

基于 Telegram bot – 机器人,可以实现发送消息消、视频、声音、文件 等功能,


今天主要讲下、PHP 来实现流程
API地址: https://core.telegram.org/api

准备工作

创建机器人

1.打开 BotFather

PC 端直接打开*** [https://telegram.me/botfather]
(https://telegram.me/botfather)
手机端搜索 BotFather

2.创建操作步骤

1.输入  /newbot
Alright, a new bot. How are we going to call it? Please choose a name for your bot.

2.输入 XXXXBot
BotFather, [15.05.19 10:46]
Done! Congratulations on your new bot. You will find it at t.me/XXXXBot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you\\'ve finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.

Use this token to access the HTTP API:
xxxxxxxxxx:yyyyyyyyyyyyyyyyyyyyyyy
Keep your token secure and store it safely, it can be used by anyone to control your bot.

For a description of the Bot API, see this page: https://core.telegram.org/bots/api

3.项目中引入 sdk(我的项目是基于 Laravel 构建的)

通过 composer 安装 sdk (当然如果是实现简单功能,可以直接使用 curl 来构造 api请求)
composer require longman/telegram-bot:^0.56

$telegram = new Longman\TelegramBot\Telegram($botToken, $botUsername);
$telegram->useGetUpdatesWithoutDatabase();
Longman\TelegramBot\Request::sendMessage([
                    \\'chat_id\\' => (int)$v,
                    \\'text\\'    => $message,
  ]);

关于 chat_id 的获取

具体操作
1.获取个人 chat_id; 需要给机器人发一条消息
2.获取群组 chat_id; 需要把 机器人拉入到群组中,如果已经拉入群组中,还是获取不到,应该是权限的问题 需把 机器人设置为管理员
3.获取频道 chat_id; 把机器人拉入频道

未经允许不得转载:破虚小站 » PHP集成Telegram Bot 机器人发送消息-详细流程

赞 (0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏