public async Task <IActionResult> PutConversationDetail(string id, ConversationDetail conversationDetail) { if (id != conversationDetail.ConversationId) { return(BadRequest()); } _context.Entry(conversationDetail).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ConversationDetailExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <MessageDetail> > SendMessage(MessageDetail messageDetail) { //Get Signalwire number TwilioClient.Init("28361e6c-85b8-40f5-bde1-bfc8cf68a96c", "PT65bfa7479efd98c38f525e7c352277e70aff63ef22f4e8be", new Dictionary <string, object> { ["signalwireSpaceUrl"] = "manish.signalwire.com" }); //Create and send outgoing message var message = MessageResource.Create( from: new Twilio.Types.PhoneNumber(messageDetail.FromPhoneNumber), body: messageDetail.Body, to: new Twilio.Types.PhoneNumber(messageDetail.ToPhoneNumber) ); messageDetail.MessageSid = message.Sid; messageDetail.Time = DateTime.UtcNow.ToString(); messageDetail.Direction = (message.Direction).ToString(); _context.MessageDetails.Add(messageDetail); await _context.SaveChangesAsync(); //Update conversation table var conversationsToUpdate = await _conversationContext.ConversationDetails.Where(a => ((a.FromPhoneNumber == messageDetail.FromPhoneNumber) && (a.ToPhoneNumber == messageDetail.ToPhoneNumber)) || ((a.FromPhoneNumber == messageDetail.ToPhoneNumber) && (a.ToPhoneNumber == messageDetail.FromPhoneNumber))).ToListAsync(); if (conversationsToUpdate.Count != 0) { foreach (ConversationDetail convo in conversationsToUpdate) { convo.LastMessage = messageDetail.Body; convo.LastMessageTime = messageDetail.Time; _conversationContext.ConversationDetails.Update(convo); } } else { ConversationDetail convo = new ConversationDetail { ConversationId = messageDetail.ToPhoneNumber + messageDetail.FromPhoneNumber, ConversationName = "", FromPhoneNumber = messageDetail.FromPhoneNumber, ToPhoneNumber = messageDetail.ToPhoneNumber, LastMessage = messageDetail.Body, LastMessageTime = messageDetail.Time }; _conversationContext.ConversationDetails.Add(convo); convo = new ConversationDetail { ConversationId = messageDetail.FromPhoneNumber + messageDetail.ToPhoneNumber, ConversationName = "", FromPhoneNumber = messageDetail.ToPhoneNumber, ToPhoneNumber = messageDetail.FromPhoneNumber, LastMessage = messageDetail.Body, LastMessageTime = messageDetail.Time }; _conversationContext.ConversationDetails.Add(convo); } await _conversationContext.SaveChangesAsync(); return(CreatedAtAction("GetMessageDetail", new { id = messageDetail.MessageSid }, messageDetail)); }