【发布时间】:2021-11-26 20:50:52
【问题描述】:
我开发了一个电报机器人,它的工作是在用户请求时向群组发送信息/消息。它是这样工作的:假设如果我在组中发布“x 球员得分”文本,它将带来该特定球员的板球得分,并且这种机制发生在无限期 while 循环(长轮询)中
问题在于,除非我也发送其他玩家的请求信息,否则它会继续带来该特定玩家的分数。所以我想要实现的是机器人应该只检查一次该消息并满足请求。
这是我的代码
$token = 'xyz';
$group_name = 'xyz';
while(true){
$get_msg = file_get_contents("https://api.telegram.org/bot%7B$token%7D/getUpdates");
$get_msg_decode = json_decode($get_msg);
$msg_array = $get_msg_decode['result'];
$last_msg_array = end($msg_array);
$last_msg = $last_msg_array['message']['text']; // this would bring the latest message from telegram group
x_player_score = 50;
// Some actions or logic
if($last_msg == x_player_score){
$bot = "https://api.telegram.org/bot{$token}/sendMessage?chat_id={$group_name}&text={x_player_score}";
$hit = file_get_contents($bot);
}
sleep(5);
}
【问题讨论】:
标签: php telegram telegram-bot php-telegram-bot