WhatsApp WebHook 被某人调用时,我如何从 WebHook 返回响应?

huangapple go评论57阅读模式
英文:

WhatsApp WebHook is called by someone how do I return a response from the WebHook?

问题

我对WhatsApp相当新,所以这可能是一个非常简单的问题。

当我的Webhook被用户调用并带有一个快速回复时,比方说一个"OK"的消息,我想要返回一个简单的文本消息,比如"很好"。应该如何实现?我需要配置一个聊天机器人吗?发送一个新的文本消息吗?

当前的控制器是asp.net MVC 5

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Hook(Root data)
{
    Entry entry = data.entry.First();
    Change change = entry.changes.First();
    List<Message> messages = change.value.messages;
    string body = messages.First().text.body;

    // 在这里返回一个真实的响应,例如:"祝你有美好的一天!"
    
    return new HttpStatusCodeResult(HttpStatusCode.OK);
}

我希望能够返回一个真实的响应,例如:"祝你有美好的一天!"。

感谢您的建议和见解。

英文:

I am fairly new to WhatsApp so this may be a very simple problem.

When my webhook gets called by a user with a quick reply, let's just say a "OK" message and I want to return a simple text message such as "Great". How is that done? Do I need to configure a ChatBot? Send a new text message?

The current controller, asp.net MVC 5

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Hook( Root data )
    {
        Entry entry = data.entry.First();
        Change change = entry.changes.First();
        List&lt;Message&gt; messages = change.value.messages;
        string body = messages.First().text.body;

		return new HttpStatusCodeResult( HttpStatusCode.OK );
    }

I would like to be able to return a real response, "Have a great day!", for example.

Suggestions and insight are appreciated.

答案1

得分: 0

你可以使用以下代码添加字符串:

return Content("Great", "text/plain");

更新

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Hook(Root data)
{
    try
    {
        Entry entry = data.entry.First();
        Change change = entry.changes.First();
        List<Message> messages = change.value.messages;
        string body = messages.First().text.body;

        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }
    catch (Exception ex)
    {
        // 记录异常错误
        return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
    }
}
英文:

You can add the string with the following code:

return Content(&quot;Great&quot;, &quot;text/plain&quot;);

Update

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Hook( Root data )
{
    try
    {
       Entry entry = data.entry.First();
       Change change = entry.changes.First();
       List&lt;Message&gt; messages = change.value.messages;
       string body = messages.First().text.body;

       return new HttpStatusCodeResult( HttpStatusCode.Ok);
    }
    catch(Exception ex)
    {
        // Log Exception Error
        return new HttpStatusCodeResult( HttpStatusCode.InternalServerError );
    }
}

huangapple
  • 本文由 发表于 2023年6月19日 01:54:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定