Example #1
0
        // HTTP Get endpoint to verify Webhook using the Verify Token
        public HttpResponseMessage Get()
        {
            var querystrings = Request.GetQueryNameValuePairs().ToDictionary(x => x.Key, x => x.Value);

            _log.Info(querystrings);
            Bot.Messenger.MessengerPlatform bot = Bot.Messenger.MessengerPlatform.CreateInstance(
                Bot.Messenger.MessengerPlatform.CreateCredentials(appSecret, pageToken, "punnel"));

            if (bot.Authenticator.VerifyToken(querystrings["hub.verify_token"]))
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(querystrings["hub.challenge"], Encoding.UTF8, "text/plain")
                });
            }

            return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
        }
Example #2
0
        public async Task <HttpResponseMessage> Post()
        {
            var body = await Request.Content.ReadAsStringAsync();

            Bot.Messenger.MessengerPlatform bot = Bot.Messenger.MessengerPlatform.CreateInstance(
                Bot.Messenger.MessengerPlatform.CreateCredentials(appSecret, pageToken, "punnel"));

            if (!bot.Authenticator.VerifySignature(Request.Headers.GetValues("X-Hub-Signature").FirstOrDefault(), body))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            Bot.Messenger.Models.WebhookModel webhookModel = bot.ProcessWebhookRequest(body);

            foreach (var entry in webhookModel.Entries)
            {
                foreach (var evt in entry.Events)
                {
                    if (evt.EventType == Bot.Messenger.Models.WebhookEventType.MessageReceivedCallback)
                    {
                        _log.InfoFormat("Id: {0} , Name: {1}", evt.Sender.ID, evt.Sender.Name);
                        _log.InfoFormat("Id: {0} , Name: {1}", evt.Recipient.ID, evt.Recipient.Name);
                        await bot.SendApi.SendActionAsync(evt.Sender.ID, Bot.Messenger.Models.SenderAction.typing_on);

                        Bot.Messenger.Models.UserProfileApiResponse userProfileRsp = await bot.UserProfileApi.GetUserProfileAsync(evt.Sender.ID);

                        _log.Info(JsonConvert.SerializeObject(evt.Message));

                        if (evt.Message.Attachments == null)
                        {
                            _log.Info(userProfileRsp?.FirstName);
                            _log.InfoFormat("txt: {0}", evt.Message.Text);

                            if (evt.Message.IsQuickReplyPostBack)
                            {
                                var c = evt.Message.QuickReplyPostback;
                                if (c.Payload == "MS_YES")
                                {
                                    await bot.SendApi.SendTextAsync(evt.Sender.ID, $"Chúc mừng, cho mình xem ảnh người yêu với {userProfileRsp?.FirstName} :)");
                                }
                                else if (c.Payload == "MS_NO")
                                {
                                    await bot.SendApi.SendTextAsync(evt.Sender.ID, $"Haha :D, mình được không?");
                                }
                                else
                                {
                                    //await bot.SendApi.SendActionAsync(evt.Sender.ID, SenderAction.typing_off);
                                    //await bot.SendApi.SendActionAsync(evt.Sender.ID, SenderAction.mark_seen);
                                }
                            }
                            else if (evt.Message.Text == string.Empty || evt.Message.Text.ToLower() == "hi" || evt.Message.Text.ToLower() == "chao" || evt.Message.Text.ToLower() == "hi")
                            {
                                //await bot.SendApi.SendTextAsync(evt.Sender.ID, $"Xin chào {userProfileRsp?.FirstName} :)");
                                var t = GenericTemplateTest();
                                _log.InfoFormat("send template: {0}", JsonConvert.SerializeObject(t));
                                var r = await bot.SendApi.SendTemplateAsync <GenericTemplate>(evt.Sender.ID, t);

                                //var r = await bot.SendApi.SendTemplateAsync<GenericTemplate>(evt.Sender.ID, t);
                                //_log.Error(r.Exception.Message);
                                //_log.Error(r.Exception);
                                //    await bot.SendApi.SendTextAsync(evt.Sender.ID, "Bạn có người yêu chưa?", new List<QuickReply>
                                //{
                                //        new QuickReply
                                //        {
                                //            ContentType = QuickReplyContentType.text,
                                //            Title = "Có rồi",
                                //            Payload = "MS_YES"
                                //        },
                                //        new QuickReply
                                //        {
                                //            ContentType = QuickReplyContentType.text,
                                //            Title = "Đang kiếm",
                                //            Payload = "MS_NO"
                                //        }
                                //});
                            }
                            else
                            {
                                await bot.SendApi.SendActionAsync(evt.Sender.ID, SenderAction.typing_off);

                                await bot.SendApi.SendActionAsync(evt.Sender.ID, SenderAction.mark_seen);
                            }
                        }
                        else // if the user sent an image, file, sticker etc., we send it back to them
                        {
                            foreach (var attachment in evt.Message.Attachments)
                            {
                                if (attachment.Type != Bot.Messenger.Models.AttachmentType.fallback &&
                                    attachment.Type != Bot.Messenger.Models.AttachmentType.location)
                                {
                                    _log.Info("gui lai");
                                    _log.Info(JsonConvert.SerializeObject(attachment));
                                    await bot.SendApi.SendTextAsync(evt.Sender.ID, $"Chào {userProfileRsp?.FirstName}, cảm ơn bạn đã gửi cái này và tôi nghĩ nó sẽ rất tuyệt, gửi lại bạn nè :)");

                                    //await bot.SendApi.SendAttachmentAsync(evt.Sender.ID, attachment);

                                    await bot.SendApi.SendAttachmentAsync(evt.Sender.ID, new Attachment <Models.MessageItem>()
                                    {
                                        Type    = AttachmentType.image,
                                        Payload = new Models.MessageItem()
                                        {
                                            url = "https://static.becungshop.vn/images/500x500/set-ao-so-mi-va-ao-tay-dai-ride-cho-be-gai-tu-5-10-tuoi-p24121705c7295-500x500.jpg"
                                        }
                                    });

                                    var t = ButtonTest();
                                    var r = await bot.SendApi.SendTemplateAsync(evt.Sender.ID, t);
                                }
                            }
                        }
                    }
                }
            }

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }