将 Facebook 按钮模板从 json 转换为 C#

Converting facebook button template from json to c#

您好,我正在尝试在我的 Bot Framework V4 的 C# 代码上执行此模板。 这是来自facebook的代码。

  "payload": {
  "template_type":"button",
  "text":"<MESSAGE_TEXT>",
  "buttons":[
    <BUTTON_OBJECT>, 
    <BUTTON_OBJECT>, 
    ...
  ]
}

这是我的尝试。我无法调试错误,因为它只适用于 Messenger。如有任何帮助,我们将不胜感激。

            Activity reply = stepContext.Context.Activity.CreateReply();
            reply.ChannelData = JObject.FromObject(
                new
                {
                    attachment = new
                    {
                        type = "template",
                        payload = new
                        {
                            template_type = "button",
                            text = "xx",
                            buttons = new[]
                            {
                               new
                                  {
                                       type = "web_url",
                                       title = "take test",
                                       url = "xx",
                                       messenger_extensions="true",
                                       webview_height_ratio = "tall",
                                  },
                            },
                        },
                    },
                });
            await stepContext.Context.SendActivityAsync(reply);

您的代码看起来不错。确保 Whitelist 您在 Facebook 上使用的任何 URL。请注意,它们必须是 https 个网址。此外,除非网站配置为 webview,否则您不需要按钮中的 messenger_extensions 和 webview_height_ratio 属性。

var reply = turnContext.Activity.CreateReply();

var attachment = new
{
    type = "template",
    payload = new
    {
        template_type = "button",
        text = "Sign up for our mailing list!",
        buttons = new[]
        {
            new
            {
                type = "web_url",
                url = "https://mybot.azurewebsites.net/",
                title = "Sign Up!"
            },
        },
    },
};

reply.ChannelData = JObject.FromObject(new { attachment });

await turnContext.SendActivityAsync(reply, cancellationToken);

查看 Button Templates 上的 Messengers 文档了解更多详细信息。

希望对您有所帮助!