从Bot框架调用FB问候语

时间:2017-03-23 21:30:27

标签: c# facebook facebook-graph-api botframework

我尝试使用C#SDK创建FB Gretting并根据FB文档发送curl请求:https://developers.facebook.com/docs/messenger-platform/thread-settings/greeting-text

然而,似乎FB没有回应。任何指针将不胜感激。感谢

public class MessagesController : ApiController
{
    int i = 0;
    /// <summary>
    /// POST: api/Messages
    /// Receive a message from a user and reply to it
    /// </summary>
    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {

        if (activity.Type == ActivityTypes.Message)
        {
            //await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
            await Conversation.SendAsync(activity, () => new Dialogs.TheQnAMakerDialog());
        }
        else
        {                
            HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

    private Activity HandleSystemMessage(Activity message)
    {
        if (message.Type == ActivityTypes.DeleteUserData)
        {
            // Implement user deletion here
            // If we handle user deletion, return a real message
        }
        else if (message.Type == ActivityTypes.ConversationUpdate)
        {
            // Handle conversation state changes, like members being added and removed
            // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
            // Not available in all channels
            var callFB=SendFBGreeting();                
        }
        else if (message.Type == ActivityTypes.ContactRelationUpdate)
        {
            // Handle add/remove from contact lists
            // Activity.From + Activity.Action represent what happened
        }
        else if (message.Type == ActivityTypes.Typing)
        {
            // Handle knowing tha the user is typing
        }
        else if (message.Type == ActivityTypes.Ping)
        {
        }

        return null;
    }

    private async Task<HttpResponseMessage> SendFBGreeting()
    {
        var payload = @"{
            ""setting_type"": ""greeting"",
            ""greeting"": {
                ""text"": ""Timeless apparel for the masses.""
            }
        }";
        var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(payload));
        var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
        using (var client = new HttpClient())
        {
            // Do the actual request and await the response
            var httpResponse = await client.PostAsync("https://graph.facebook.com/v2.6/me/thread_settings?access_token=xxx", httpContent);

            // If the response contains content we want to read it!
            if (httpResponse.Content != null)
            {
                var responseContent = await httpResponse.Content.ReadAsStringAsync();

            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

Facebook不返回 ConversationUpdate 事件。尝试在其他地方调用 SendFBGreeting()

请紧记问候文字之前用户入门,因此,即使您确实收到了ConversationUpdate事件,在这种情况下也没有关系。 / p>