简体   繁体   English

如何在电报Bot中使用ForceReply

[英]How to use ForceReply in a telegram bot

I'm developing a telegram bot using php and a web hook. 我正在使用php和网络挂钩开发电报机器人。 All it's fine but sometimes I would like to "wait for a reply" from the user. 一切都很好,但有时我想“等待用户回复”。 For example: 例如:

If the client write /info without any parameters I would like to show a "usage" message and ask&wait for a ID parameter. 如果客户端写的/ info没有任何参数,我想显示“使用情况”消息并询问并等待ID参数。

I know there is a property "ForceReply" to force for a reply, but when I set it up nothing happens, and I don't know how to know if the client message its a reply for my question. 我知道有一个属性“ ForceReply”可以强制答复,但是当我设置它时,什么也没有发生,而且我不知道如何知道客户是否向我的问题发送了答复。

Do I have to put my php server on hold? 我必须暂停我的php服务器吗? (I think it would be a bad practice) Do I have to whait for a type of message? (我认为这是一种不好的做法)我是否需要等待某种消息?

Thanks 谢谢

When you use getUpdates or receive updates via a webhook, the update message will contain a field like reply_to_message field. 当您使用getUpdates或通过Webhook接收更新时,更新消息将包含诸如reply_to_message字段之类的字段。 You can use this to compare it to the message you sent. 您可以使用它来将其与您发送的消息进行比较。

If you are running your script via webhooks I would assume that it only executes when it receives a message. 如果您是通过webhooks运行脚本的,那么我认为它仅在收到消息时执行。 If so, I would suggest you use something like memcache/redis to store the message you are expecting a reply for and then when the reply comes, you can compare it to the value stored: 如果是这样,我建议您使用诸如memcache / redis之类的东西来存储您希望得到答复的消息,然后在答复出现时,可以将其与存储的值进行比较:

<?php
// This script triggers as a webhook
$message = file_get_contents('php://input');
$message = json_decode($message, true);
$cache = new RedisClient('localhost');

if ($message->reply_to_message == $cache->get('original.message.id'))
{
    var_dump('message reply received');
}

The example above is some "pseudo" code that you can use in a webhook to check for a reply to a specific message. 上面的示例是一些“伪”代码,您可以在Webhook中使用该代码检查对特定消息的答复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM