Example #1
0
 public async Task AddMessagesAsync(Guid chatId, ICollection <MessageModel> messages)
 {
     await _chatRepository.AddMessagesAsync(chatId, _mapper.Map <ICollection <Message> >(messages));
 }
Example #2
0
        public async Task SendToChatBot(string conversationId, string message)
        {
            List <Message> messages = new List <Message>();
            string         from     = GetConnectionKey();

            await DisplayMessage(ChatBot, from, message);



            if (Int32.TryParse(conversationId, out int id))
            {
                Message msg = new Message
                {
                    ConversationId = id,
                    To             = ChatBot,
                    From           = from,
                    DisplayName    = await GetDisplayName(),
                    Content        = message,
                    IsChatBot      = false,
                    IsChatWorker   = false,
                    DateTime       = DateTime.Now
                };

                messages.Add(msg);

                try
                {
                    string response = await _chatBotRepository.PostToActiveKnowledgeBase(message);

                    Message responseMsg = new Message
                    {
                        ConversationId = id,
                        From           = ChatBot,
                        DisplayName    = ChatBot,
                        Content        = response,
                        IsChatBot      = true,
                        IsChatWorker   = false,
                        DateTime       = DateTime.Now
                    };


                    messages.Add(responseMsg);

                    if (!await ResponseIsKeyword(response, id))
                    {
                        // Display response message
                        await DisplayMessage(from, ChatBot, response);
                    }
                }
                catch (Exception exception)
                {
                    await DisplayChatBotConnectionError(from);
                }
            }
            else
            {
                await DisplayChatBotConnectionError(from);
            }


            try
            {
                // Save messages
                await _chatRepository.AddMessagesAsync(messages);
            }
            catch (Exception exception)
            {
                // ChatBot should continue to work even if messages does not get stored to db.
                // So do nothing here.
            }
        }