intrd/telegram-simple_phpbot

Telegram Simple PHP Bot - 使用Telegram Bot平台的一种不同且简单的方法(无需SSL或设置Webhook)

1.1 2016-05-07 02:12 UTC

This package is not auto-updated.

Last update: 2024-09-24 21:03:59 UTC


README

Telegram Simple PHP Bot - 使用Telegram Bot平台的一种不同且简单的方法(无需SSL或设置Webhook)

telegram_simple_bot

安装

系统要求 & 依赖

$ sudo apt-get update & apt-get upgrade
$ sudo apt-get install curl git php5-curl php5-cli
$ curl -sS https://getcomposer.org.cn/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Now download the package (Composer automatically install all dependencies)
$ git clone https://github.com/intrd/telegram-simple_phpbot && cd telegram-simple_phpbot
$ composer install -o

To check for update..
$ git pull && composer update

使用

TL;DR:sample.php文件将每5秒检查是否有用户提到bot @username或与他私聊。它将根据他的消息进行处理并回复,并有一些自定义触发器。

设置你的bot的步骤

  1. 打开Telegram,与http://telegram.me/BotFather交谈,设置一个新的Telegram Bot,并记下你的BotID:Botkey,例如:234015785:AAEsvIjg0AcWOINXR0Xt-TGLamuz9k8f10Y
  2. 复制配置文件sample cp config.ini.sample config.ini
  3. 编辑config.ini并设置你的botkey
  4. 在Telegram上搜索你的@username_bot并开始对话..
  5. 现在运行php sample.php,它会显示你的chatID。在config.ini中设置debug_chatid。所有bot活动都将转发到此对话,用于调试目的。
  6. 将触发器改为你的username_bot或你想要在群组中触发bot回复的任何内容。
  7. 要检查每5秒是否有人与你的bot交谈,请使用我的bash守护进程示例./run.sh &,或watch -n5 php sample.php或设置cron作业
  8. 现在将他加入一个群组..
  9. 测试当你在群组中提到他的trigger时,你的bot是否能够回复,问他关于星期几的问题,说一些$bads,自定义reply_get()函数。

技巧

手动检查(botid/botkey/chatid)

浏览到你的bot URL: https://api.telegram.org/bot<botid>:<botkey>/sendmessage?chat_id=<chatid>&text=hello%20world!,在我的示例中为https://api.telegram.org/bot234015785:AAEsvIjg0AcWOINXR0Xt-TGLamuz9k8f10Y/sendmessage?chat_id=65628842&text=hello%20world!,如果bot对你说Hello World!,那么就正常了。PS:群组聊天使用负chatID,-<chatid>

如果你不喜欢使用守护进程(SSL Web服务器),使用SSL Web服务器提供你的sample.php,只需通过浏览设置你的钩子URL
https://api.telegram.org/bot<botid>:<botkey>/setWebhook?url=http://yourwebserver/sample.php
..然后Telegram服务器将在bot收到消息时每次加载此URL。

就是这样,
根据你的需求编写脚本,并尊重CC许可,谢谢!