Example #1
0
        public async Task PostAsync([FromBody] JObject MessageJobj)
        {
            bool   outgoing;
            string ChannelUUId = "";

            string SocketPublishUrl = _configuration.GetValue <string>("RequestUrls:WebSocketPublish");

            string MdeiaType = "0";

            LogProperties.info("Whats api called");


            try

            {
                outgoing = (bool)MessageJobj["outgoing"];
                //               "sender": "919966226645",
                //"recipient": "2df4b63c-135a-4206-91d3-8132dfe64f22",
                if (!outgoing)
                {
                    ChannelUUId = Convert.ToString(MessageJobj["recipient"]);
                }

                WidgetChannels widgetChannel = null;
                Widgets        widget        = null;
                try
                {
                    widgetChannel = _context.WidgetChannels.Where(wc => wc.ChanneUUID == ChannelUUId && wc.ConversationTypeId == 2).FirstOrDefault();
                    LogProperties.info("widgetChannel for message: " + widgetChannel.WidgetId);

                    widget = _context.Widgets.Where(w => w.Id == widgetChannel.WidgetId).FirstOrDefault();
                    LogProperties.info("widget for message: " + widgetChannel.WidgetId);
                }
                catch (Exception dbEx)
                {
                    widgetChannel = _context.WidgetChannels.Where(wc => wc.Id == 2).FirstOrDefault();
                    LogProperties.info("widgetChannel for message catch: " + widgetChannel.WidgetId);

                    widget = _context.Widgets.Where(w => w.Id == 6).FirstOrDefault();
                    LogProperties.info("widget for message catch: " + widget.Id);
                }
                // var widgetChannel = _context.WidgetChannels.Where(wc => wc.ChanneUUID == ChannelUUId && wc.ConversationTypeId == 2).FirstOrDefault();
                // LogProperties.info("widgetChannel for message: " + widgetChannel.WidgetId );



                //[HttpPost("{account_id}/widgets/{widget_id}/conversations/{conversation_id}/{is_endchat}/replies")]
                if (widget != null)
                {
                    if (!outgoing)
                    {
                        JObject jObject = new JObject();
                        jObject = JObject.Parse(MessageJobj.ToString());
                        //    await ProcessIncomingMessageAsync(widget, MessageJobj);

                        LogProperties.info("Whats api ougoing enter");
                        string CustomerMobile       = "";
                        string CustomerName         = "";
                        string CustomerProfileImage = "";

                        int     AccountId              = widget.AccountId;
                        int     WidgetId               = widget.Id;
                        int     ConverstationId        = 0;
                        long    ConverstationMessageId = 0;
                        int     CustomerId             = 0;
                        JObject CustJobj               = new JObject();

                        CustomerMobile       = Convert.ToString(MessageJobj["sender"]);
                        ChannelUUId          = Convert.ToString(MessageJobj["recipient"]);
                        CustomerName         = Convert.ToString(MessageJobj["payload"]["user"]["name"]);
                        CustomerProfileImage = Convert.ToString(MessageJobj["payload"]["user"]["image"]);

                        ConversationsController conController = new ConversationsController(_context, _configuration);



                        CustJobj   = GetCustomerId(AccountId, CustomerName, CustomerMobile, CustomerProfileImage);
                        CustomerId = Convert.ToBoolean(CustJobj["Success"]) ? Convert.ToInt32(CustJobj["CustomerId"]) : -1;
                        LogProperties.info("Whats api after get customer");
                        var PrevConverstations = (from Con in _context.Conversations
                                                  where Con.WidgetId == WidgetId && Con.CustomerId == CustomerId
                                                  orderby Con.Id descending
                                                  select new {
                            Con.Id,
                            Con.AgentId
                        }).FirstOrDefault();

                        if (PrevConverstations != null)
                        {
                            ConverstationId = PrevConverstations.Id;
                        }
                        LogProperties.info("Whats api after Conversations");
                        var PrevConMessages = (from ConMess in _context.ConversationMessages
                                               where ConMess.ConversationId == ConverstationId
                                               orderby ConMess.Id descending
                                               select new
                        {
                            ConMess.Id,
                            ConMess.CreatedTimeUTC,
                            ConMess.AgentId
                        }).FirstOrDefault();

                        DateTime RecentMsgTime;
                        if (PrevConMessages != null && PrevConMessages.Id > 0)
                        {
                            ConverstationMessageId = PrevConMessages.Id;
                            RecentMsgTime          = PrevConMessages.CreatedTimeUTC;
                        }
                        else
                        {
                            RecentMsgTime = DateTime.Today.AddDays(-10);
                        }

                        LogProperties.info("Whats api after Conversation messages");

                        DateTime CurrentUtcNow = DateTime.UtcNow;
                        TimeSpan MessageSpan   = CurrentUtcNow - RecentMsgTime;


                        double totalSecForDay = 3 * 60 * 60;           // Static condiftion
                        bool   IsNewChat      = false;
                        if (MessageSpan.TotalSeconds > totalSecForDay) // create new conversations after 24 hours
                        {
                            Conversations conversations = new Conversations()
                            {
                                StatusId           = 1,
                                CustomerId         = CustomerId,
                                Mobile             = CustomerMobile,
                                ConversationTypeId = 2
                            };


                            ActionResult <Conversations> newCon = await conController.PostConversations(AccountId, WidgetId, conversations);

                            // JObject Con = await CreateConversation(AccountId, WidgetId, conversations);

                            OkObjectResult okResult = newCon.Result as OkObjectResult;

                            if (okResult.StatusCode == 200)
                            {
                                Conversations NewConversation = okResult.Value as Conversations;
                                ConverstationId = NewConversation.Id;
                            }



                            IsNewChat = true;
                        }
                        LogProperties.info("Whats api after Conversation save");
                        ConversationMessages conversationMessages = new ConversationMessages();
                        int IsBotEnd = 0;
                        //"MessageTypeId": "1",
                        //    "Message":msg
                        conversationMessages.ConversationId     = ConverstationId;
                        conversationMessages.MessageTypeId      = 1;
                        conversationMessages.ConversationTypeId = 2;

                        if (IsNewChat == false)
                        {
                            conversationMessages.AgentId = PrevConverstations.AgentId;
                        }
                        //message payload table
                        conversationMessages.Message = Convert.ToString(MessageJobj["payload"]["text"]);
                        string CustomerMessage = Convert.ToString(MessageJobj["payload"]["text"]);

                        string MessageType = Convert.ToString(MessageJobj["payload"]["type"]);


                        if (MessageType != "text")
                        {
                            //attachment

                            conversationMessages.AttachmentUrl = Convert.ToString(MessageJobj["payload"]["attachment"]);

                            conversationMessages.Message = ConstructMediaMessage(conversationMessages.AttachmentUrl, MessageType, conversationMessages.Message, ref MdeiaType);
                        }


                        var ConInfo = _context.ConversationInfo.Where(w => w.ConversationId == ConverstationId).FirstOrDefault();
                        if (ConInfo != null)
                        {
                            IsBotEnd = ConInfo.IsBotEnd;
                        }
                        if (IsBotEnd > 0)
                        {
                            conversationMessages.MessageTypeId = 3;
                        }
                        ActionResult <ConversationMessages> newConMsg = await conController.PostConversationMessages(AccountId, WidgetId, ConverstationId, IsBotEnd, conversationMessages);



                        OkObjectResult okResultMesg = newConMsg.Result as OkObjectResult;

                        if (okResultMesg.StatusCode == 200)
                        {
                            ConversationMessages NewConvMsg = okResultMesg.Value as ConversationMessages;
                        }

                        LogProperties.info("Whats api after Conversation message save");

                        if (IsBotEnd > 0)
                        {
                            // table for botend

                            //if (IsPingAgent == "true")
                            //{
                            //    cust_jsonObj = { "conversationId": conversationId, "from": conversationId, "to": agentId, "messageType": "0", "message": "Customer OnBoard" };
                            //}
                            //else
                            //{
                            //    cust_jsonObj = { "conversationId": conversationId, "from": fromId, "to": agentId, "messageType": "0", "message": "Customer OnBoard" };
                            //}
                            if (IsNewChat == false && IsBotEnd > 0) // send message to agent through socket
                            {
                                //JObject jobj = new JObject();
                                //jobj.Add(new JProperty("conversationId", conversationMessages.ConversationId));
                                //jobj.Add(new JProperty("from", "WhatsApp_"+ conversationMessages.ConversationId));
                                //jobj.Add(new JProperty("to", "Hub_" + conversationMessages.AgentId));
                                //jobj.Add(new JProperty("message", conversationMessages.Message));
                                //jobj.Add(new JProperty("messageType", "0"));

                                //WebSocketSubscriber.Send(jobj);

                                JObject jobjs        = new JObject();
                                JObject wsMessageObj = new JObject();


                                JObject CustomerDetails = new JObject();

                                //                "data":{"fromName" :"" ,"fromMobile",""}

                                CustomerDetails.Add(new JProperty("fromName", CustomerName));
                                CustomerDetails.Add(new JProperty("fromMobile", CustomerMobile));

                                jobjs.Add(new JProperty("conversationId", Convert.ToString(ConverstationId)));
                                jobjs.Add(new JProperty("from", "WhatsApp_5f2d3a8e31197a445686653b"));
                                jobjs.Add(new JProperty("to", "Hub_" + PrevConverstations.AgentId));
                                jobjs.Add(new JProperty("message", CustomerMessage));
                                jobjs.Add(new JProperty("mediaUrl", conversationMessages.AttachmentUrl));
                                jobjs.Add(new JProperty("type", MdeiaType));
                                jobjs.Add(new JProperty("data", CustomerDetails));
                                jobjs.Add(new JProperty("messageType", "0"));
                                jobjs.Add(new JProperty("ConversationTypeId", 2));

                                wsMessageObj = new JObject(new JProperty("Module", "Chat"),
                                                           new JProperty("Event", "NewChat"),
                                                           new JProperty("Channel_Name", "WhatsApp_Hub_" + PrevConverstations.AgentId),
                                                           new JProperty("Data", jobjs));


                                // WebSocketSubscriber wb = new WebSocketSubscriber();
                                WebSocketSubscriber.pushSocket(SocketPublishUrl, wsMessageObj);

                                LogProperties.info("Whats api after websocket publish");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogProperties.error("expection at WhatsApp Handler: " + ex.ToString());
            }
        }
Example #2
0
 public string GetWelcomeMsg()
 {
     LogProperties.info("Application started");
     return("!!!Welcome to TelebuHubChat!!!");
 }
Example #3
0
 public IEnumerable <string> Get()
 {
     LogProperties.info("Whats api get called");
     return(new string[] { "value1", "value2" });
 }