iiiohsen / telegram-bot
Duhow PHP Telegram bot
dev-my_branch
2018-02-04 16:01 UTC
Requires
- php: ^5.5|^7.0
- ext-curl: *
This package is not auto-updated.
Last update: 2024-09-20 07:52:45 UTC
README
用于使用 PHP 与 Telegram 机器人交互的另一个库。
- 包含 src/Autoloader.php 文件。
- 创建一个 Telegram\Bot 对象。
- 使用 $bot 创建一个 Telegram\Receiver 对象。
$bot = new Telegram\Bot("11111111:AAAAAAAAAAzzzzzzzzzzzzzzzzzzz", "MyUserBot", "The Name of Bot"); $tg = new Telegram\Receiver($bot);
您可以创建任意数量的 Bots、Receivers 或 Senders。使用 Receiver 包括 Sender。
用法
页面加载后(手动或通过 webhook),您可以发送或回复请求。
向用户或群聊发送消息
$tg->send ->chat("123456") ->text("Hello world!") ->send();
回复用户命令
if($tg->text_command("start")){ $tg->send ->text("Hi!") ->send(); }
回复用户消息
if($tg->text_has("are you alive")){ $tg->send ->text("Yes!") ->send(); }
新功能: 解析字符串
if($tg->text_regex("I'm {N:age}") and $tg->words() <= 4){ $num = $tg->input->age; $str = "So old..."; if($num < 18){ $str = "You're young!"; } $tg->send ->text($str) ->send(); }elseif($tg->text_regex("My name's {name}")){ $tg->send ->text("Nice to meet you, " .$tg->input->name ."!") ->send(); }
发送内嵌键盘并解析它
if($tg->callback == "but 1"){ $tg->answer_if_callback(""); // Stop loading button. $tg->send ->message(TRUE) ->chat(TRUE) ->text("You pressed the first button!") ->edit("text"); }elseif($tg->callback == "but 2"){ $tg->answer_if_callback("You pressed the second button!", TRUE); // Display an alert and stop loading button. } if($tg->text_has("matrix") and $tg->words() <= 5){ $tg->send ->text("Red or blue. You choose.") ->inline_keyboard() ->row() ->button("Red", "but 1") ->button("Blue", "but 2") ->end_row() ->show() ->send(); }
示例
- Profesor Oak,一个 Pokemon GO 的助手。