【问题标题】:Converting facebook button template from json to c#将 facebook 按钮模板从 json 转换为 c#
【发布时间】:2019-05-24 01:29:06
【问题描述】:

您好,我正在尝试在我的 C# 代码上为 Bot Framework V4 执行此模板。 这是来自 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);

【问题讨论】:

  • 这段代码遇到了什么问题?
  • 请不要将代码粘贴为图片。粘贴实际代码,以便 a) 可搜索,b) 人们可以复制和粘贴以在他们的环境中测试您的代码。
  • 对不起,我现在要编辑它。
  • @ChetanRanpariya 我的格式错误。我无法调试确切的问题,因为它没有在本地模拟器上产生错误。仅在信使中。
  • @user10860402 看到这篇文章我认为它可以帮助你:stackoverflow.com/a/44968612/9940803stackoverflow.com/a/45894182/9940803

标签: c# botframework messenger


【解决方案1】:

您的代码看起来不错。确保 Whitelist 您在 Facebook 中使用的任何 URL。请注意,它们必须是 https URL。此外,除非网站配置为 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 上的 Messenger 文档以了解更多详细信息。

希望这会有所帮助!

【讨论】:

  • 谢谢。先生,在您显示的链接中。网页是否需要托管?或者您可以只使用未托管的 html 吗?谢谢。
  • 很遗憾,该网站需要托管。
  • 我无法让它工作。看起来 Facebook 可能已经改变了一些事情。这对我有用。 stackoverflow.com/questions/68685181/…
猜你喜欢
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多