Ejemplo n.º 1
0
        public string HandleMessage(SlackMessage message)
        {
            try
            {
                this.unitOfWork.SiteLogs.Add(
                    new SiteLog
                    {
                        DateOfLog = DateTime.Now,
                        Log = string.Format("Question:{0}", JsonConvert.SerializeObject(
                                message,
                                Formatting.Indented)),
                        LogType = LogType.Log
                    });
                this.unitOfWork.SaveChanges();

                var answer = MakePigLatin(message.Text.Replace(message.TriggerWord, string.Empty));

                this.unitOfWork.SiteLogs.Add(
                    new SiteLog
                    {
                        DateOfLog = DateTime.Now,
                        Log = "Answer: " + answer,
                        LogType = LogType.Log
                    });
                this.unitOfWork.SaveChanges();

                return new { text = answer }.ToJson();
            }
            catch (Exception ex)
            {
                this.unitOfWork.SiteLogs.Add(new SiteLog { DateOfLog = DateTime.Now, Log = ex.Message, LogType = LogType.Error });
                this.unitOfWork.SaveChanges();
                throw;
            }
        }
Ejemplo n.º 2
0
        public string HandleMessage(SlackMessage message)
        {
            try
            {
                this.unitOfWork.SiteLogs.Add(
                    new SiteLog
                    {
                        DateOfLog = DateTime.Now,
                        Log = string.Format("Question:{0}", JsonConvert.SerializeObject(
                                    message,
                                    Formatting.Indented)),
                        LogType = LogType.Log
                    });
                this.unitOfWork.SaveChanges();

                QueryResult queryResult = this.wolframApi.Query(message.Text.Replace(message.TriggerWord, "").Trim());
                var answer = string.Join(" ", queryResult.Pods.Where(pod => pod.Title == "Result").SelectMany(pod => pod.SubPods.Select(subpod => subpod.Plaintext)));
                if (string.IsNullOrWhiteSpace(answer))
                {
                    answer = queryResult.Pods.Select(pod => pod.SubPods.Select(subpod => subpod.Plaintext).FirstOrDefault()).FirstOrDefault();
                }

                this.unitOfWork.SiteLogs.Add(
                    new SiteLog
                    {
                        DateOfLog = DateTime.Now,
                        Log = "Answer: " + JsonConvert.SerializeObject(queryResult, Formatting.Indented),
                        LogType = LogType.Log
                    });
                this.unitOfWork.SaveChanges();

                answer = string.IsNullOrEmpty(answer) ? "I don't seem to know the answer to that" : answer;

                return new { text = answer }.ToJson();
            }
            catch (Exception ex)
            {
                this.unitOfWork.SiteLogs.Add(new SiteLog { DateOfLog = DateTime.UtcNow, Log = ex.Message, LogType = LogType.Error });
                this.unitOfWork.SaveChanges();
                throw;
            }
        }
Ejemplo n.º 3
0
 public void ProcessRequest(HttpContext context)
 {
     if (!string.IsNullOrEmpty(context.Request.Form["text"]))
     {
         var message = new SlackMessage
                           {
                                             ChannelId = context.Request.Form["channel_id"],
                                             ChannelName = context.Request.Form["channel_name"],
                                             ServiceId = context.Request.Form["service_id"],
                                             TeamDomain = context.Request.Form["team_domain"],
                                             TeamId = context.Request.Form["team_id"],
                                             Text = context.Request.Form["text"],
                                             Timestamp = context.Request.Form["timestamp"],
                                             Token = context.Request.Form["token"],
                                             TriggerWord = context.Request.Form["trigger_word"],
                                             UserId = context.Request.Form["user_id"],
                                             UserName = context.Request.Form["user_name"],
                           };
         context.Response.Write(_slackBot.HandleMessage(message));
     }
 }