Ejemplo n.º 1
0
        public async Task <IActionResult> Post()
        {
            try
            {
                var    signature = Request.Headers["X-Hub-Signature"].FirstOrDefault().Replace("sha1=", "");
                string body      = await new StreamReader(Request.Body).ReadToEndAsync();

                if (!FacebookChatbotHelper.VerifySignature(_appSettings, signature, body))
                {
                    return(BadRequest());
                }

                var value = JsonConvert.DeserializeObject <FacebookMessage>(body);
                if (value._object != "page")
                {
                    return(Ok());
                }

                foreach (var entry in value.entry)
                {
                    // To-do: retrieve pageToken from database based on Page ID
                    var incomingPageId = entry.id;
                    var company        = _companyService.Get(incomingPageId);

                    // If company registered in db
                    if (company != null)
                    {
                        foreach (var msgItem in entry.messaging)
                        {
                            if (msgItem.message == null && msgItem.postback == null)
                            {
                                if (!FacebookChatbotHelper.VerifyIsOneTimeNotifPayload(msgItem))
                                {
                                    continue;
                                }

                                await _oneTimeNotifService.UserOptinCaseNoification(msgItem, company);
                            }
                            else
                            {
                                // Dispatch bot agent
                                await _botService.DispatchAgent(msgItem, company);
                            }
                        }
                    }
                    else
                    {
                        // To-do: Company not registered in db
                    }
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, _logger, this.Request, this.RouteData);
                return(Ok());
            }
        }