/// <summary> /// Converts an Activity object to a Facebook messenger outbound message ready for the API. /// </summary> /// <param name="activity">The activity to be converted to Facebook message.</param> /// <returns>The resulting message.</returns> public static FacebookMessage ActivityToFacebook(Activity activity) { if (activity == null) { throw new ArgumentNullException(nameof(activity)); } var facebookMessage = new FacebookMessage(activity.Conversation.Id, new Message(), "RESPONSE"); facebookMessage.Message.Text = activity.Text; if (activity.ChannelData != null) { facebookMessage = activity.GetChannelData <FacebookMessage>(); if (facebookMessage.SenderAction != null) { facebookMessage.Message = null; } else { // make sure the quick reply has a type if (facebookMessage.Message.QuickReplies.Any()) { foreach (var reply in facebookMessage.Message.QuickReplies) { if (string.IsNullOrWhiteSpace(reply.ContentType)) { reply.ContentType = "text"; } } } } } if (activity.Attachments != null && activity.Attachments.Count > 0) { var payload = JsonConvert.DeserializeObject <AttachmentPayload>(JsonConvert.SerializeObject( activity.Attachments[0].Content, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, })); var facebookAttachment = new FacebookAttachment { Type = activity.Attachments[0].ContentType, Payload = payload, }; facebookMessage.Message.Attachment = facebookAttachment; } return(facebookMessage); }
public async Task <ThreadProfileModel> GetThreadProfile(string business_id, string id) { ThreadProfileModel model = new ThreadProfileModel(); //var key = "GetThreadProfile" + business_id + id; //var lst = await CacheBase.cacheManagerGet<ThreadProfileModel>(key); //if (lst != null) // return lst; var thread = _threadService.GetById(business_id, id); var channel = _channelService.GetById(business_id, thread.channel_id); try { if (channel.type != "facebook") { return(model); } } catch {} try { model.last_visits = string.IsNullOrWhiteSpace(thread.last_visits) ? new List <string>() : JsonConvert.DeserializeObject <List <string> >(thread.last_visits); model.post = null; try { if (thread.link_ext_id != null) { var pid = thread.link_ext_id.Contains('_') ? thread.link_ext_id : string.Format("{0}_{1}", thread.channel_ext_id, thread.link_ext_id); //var key1 = "GetThreadProfile_link_ext_id" + pid; //var lst1 = await CacheBase.cacheManagerGet<PostModel>(key1); //if (lst1 != null || (lst1 == null && await CacheBase.cacheCheckExist(key1))) // model.post = lst1; //else //{ //var options = new FindOptions<FacebookPost>(); ////options.Projection = "{_id: 0}"; //options.Limit = 1; //var query = "{id:\"" + pid + "\"}"; //var rs = await _facebookConversationService.GetDataMongo(query, options, "Posts"); //FacebookPost post = null; //if (rs == null || rs.Count == 0) //{ var post = await _facebookService.GetPost(pid, channel.token); //if (post != null) //{ // var option = new UpdateOptions { IsUpsert = true }; // post._id = pid; // var filter = Builders<FacebookPost>.Filter.Where(x => x.id == pid); // _facebookConversationService.UpsertAnyMongo<FacebookPost>(post, option, filter, "Posts"); //} //} //else post = rs[0]; FacebookAttachment a = post.attachments != null && post.attachments.data != null && post.attachments.data.Count() > 0 && post.attachments.data.First() != null?post.attachments.data.First() : null; model.post = new PostModel { id = post.id, link = post.link, message = post.message, picture = post.picture, parent_id = post.parent_id, permalink_url = post.permalink_url, source = post.source, type = post.type, page_name = channel.name, created_time = post.created_time, updated_time = post.updated_time, attachments = a != null && a.subattachments != null && a.subattachments.data != null && a.subattachments.data != null ? a.subattachments.data : new List <FacebookAttachment>() }; // CacheBase.cacheManagerSetForProceduce(key1, model.post, DateTime.Now.AddDays(1), null, null, false, "select", false); //} } } catch { } if (model.post == null) { var cid = thread.owner_last_message_ext_id.Contains('_') ? thread.owner_last_message_ext_id : string.Format("{0}_{1}", thread.channel_ext_id, thread.owner_last_message_ext_id); //var key2 = "GetThreadProfile_owner_last_message_ext_id" + cid; //var lst2 = await CacheBase.cacheManagerGet<PostModel>(key2); //if (lst2 != null || (lst2 == null && await CacheBase.cacheCheckExist(key2))) // model.post = lst2; //else //{ //var options = new FindOptions<FacebookComment>(); ////options.Projection = "{_id: 0}"; //options.Limit = 1; //var query = "{id:\"" + cid + "\"}"; //var rs = await _facebookConversationService.GetDataMongo(query, options, "Posts"); //FacebookComment comment = null; //if (rs == null || rs.Count == 0) //{ var comment = await _facebookService.GetComment(cid, channel.token); //if (comment != null) //{ // var option = new UpdateOptions { IsUpsert = true }; // comment._id = cid; // var filter = Builders<FacebookComment>.Filter.Where(x => x.id == cid); // _facebookConversationService.UpsertAnyMongo<FacebookComment>(comment, option, filter, "Posts"); //} //} //} //else comment = rs[0]; if (comment != null && comment.@object != null && !string.IsNullOrWhiteSpace([email protected])) { var wid = [email protected]('_') ? [email protected] : string.Format("{0}_{1}", thread.channel_ext_id, [email protected]); var website = await _facebookService.GetWebsite(wid, channel.token); if (website != null) { model.post = new PostModel { id = comment.id, parent_id = website.id, page_name = channel.name, link = website.application.url, message = website.title, picture = website.image != null && website.image.Count() > 0 ? website.image.First().url : "", created_time = comment.created_time, updated_time = comment.created_time }; } //} //CacheBase.cacheManagerSetForProceduce(key2, model.post, DateTime.Now.AddDays(1), null, null, false, "select", false); } } } catch (Exception ex) { _logService.Create(new Log { message = ex.Message, category = "Thread", link = $"{Request.HttpContext.Request.Scheme}://{Request.HttpContext.Request.Host}{Request.HttpContext.Request.Path}{Request.HttpContext.Request.QueryString}", details = JsonConvert.SerializeObject(ex.StackTrace), name = string.Format("Get thread profile for thread id: {0}", id) }); } // CacheBase.cacheManagerSetForProceduce(key, model, DateTime.Now.AddMinutes(10), null, null, false); return(model); }