Microsoft bot框架和Messanger移动通知

时间:2017-01-16 18:17:41

标签: facebook botframework facebook-messenger facebook-messenger-bot

我已经使用Microsoft bot框架为Messenger创建了一个机器人。 一切都很棒。我可以接收并发送消息给messanger,但在messanger移动推送通知不起作用。我省略了属性notification_type,因为facebook指南说

  

notification_type是可选的;默认情况下,消息将是REGULAR   推送通知类型

这是一个框架错误?

我的代码:

    XMPPTCPConnectionConfiguration userBasicConfiguration = XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
            .setServiceName(SERVICE_NAME)
            .setHost(HOST_NAME)
            .setDebuggerEnabled(false)
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .setPort(PORT)
            .setUsernameAndPassword(userName, password)
            .build();
    AbstractXMPPConnection firstUserConnection = new XMPPTCPConnection(userBasicConfiguration);
    firstUserConnection.connect();
    StanzaFilter filter = new StanzaTypeFilter(Message.class);
    StanzaListener listener = stanza -> {
        System.out.println(stanza.toString());
    };
    firstUserConnection.addSyncStanzaListener(listener,filter);
    firstUserConnection.login();
    while (!Thread.currentThread().isInterrupted()){

    }
    firstUserConnection.disconnect();

2 个答案:

答案 0 :(得分:1)

我已经使用了activity.ChannelData并且所有工作都很好 我发布我的解决方案,对某人有用

将附件添加到活动:

activity.ChannelData = new FacebookChannelData()
{
    Attachment = GetFacebookAttachment()
};

创建附件:

private static FacebookAttachment GetFacebookAttachment()
{
    return new FacebookAttachment()
    {
        Payload = new GenericTemplate
        {
            Elements = new[] {
                    new TemplateElements(){
                    Title = "my title",
                    ItemUrl = "https://example.com",
                    ImageUrl = "https://example.com/test.jpg",
                    Subtitle = "subtitle",
                    Buttons = new[] {
                        new TemplateButtons() {
                            Type = "web_url",
                            Url = "https://example.com",
                            Title = "button title"
                        }
                    }
                    }
                }
        }
    };
}

然后是课程:

public class FacebookChannelData
{
    public FacebookChannelData() {
        this.NotificationType = "REGULAR";
    }

    [JsonProperty("notification_type")]
    public string NotificationType { get; set; }

    [JsonProperty("attachment")]
    public FacebookAttachment Attachment { get; internal set; }
}

public class FacebookAttachment
{
    public FacebookAttachment()
    {
        this.Type = "template";
    }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("payload")]
    public dynamic Payload { get; set; }

    public override string ToString()
    {
        return this.Payload.ToString();
    }
}

public class GenericTemplate
{
    public GenericTemplate()
    {
        this.TemplateType = "generic";
    }

    [JsonProperty("template_type")]
    public string TemplateType { get; set; }

    [JsonProperty("elements")]
    public TemplateElements[] Elements { get; set; }
}

public class TemplateElements
{
    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("item_url")]
    public string ItemUrl { get; set; }

    [JsonProperty("image_url")]
    public string ImageUrl { get; set; }

    [JsonProperty("subtitle")]
    public string Subtitle { get; set; }

    [JsonProperty("buttons")]
    public TemplateButtons[] Buttons { get; set; }
}

public class TemplateButtons
{
    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("url")]
    public string Url { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("payload")]
    public string Payload { get; set; }
}

答案 1 :(得分:1)

notification_type是可选的 - 但仅限于您实际在活动中指定ChannelData的情况。

所以只需添加(假设您using Newtonsoft.Json.Linq;

        activity.ChannelData = JObject.FromObject(new
        {
            notification_type = "REGULAR"
        });

如果您的客户端应用尚未停用通知,您就会收到通知。