public static FB_Marketplace _from_graphql(JToken data) { if (data.get("image") == null) { data["image"] = new JObject(new JProperty("uri", "")); } var c_info = FB_Group._parse_customization_info(data); var last_message_timestamp = data.get("last_message")?.get("nodes")?.FirstOrDefault()?.get("timestamp_precise")?.Value <string>(); var plan = data.get("event_reminders")?.get("nodes")?.FirstOrDefault() != null?FB_Plan._from_graphql(data.get("event_reminders")?.get("nodes")?.FirstOrDefault()) : null; return(new FB_Marketplace( uid: data.get("thread_key")?.get("thread_fbid")?.Value <string>(), participants: new HashSet <string>(data.get("all_participants")?.get("nodes")?.Select(node => node.get("messaging_actor")?.get("id")?.Value <string>())), nicknames: (Dictionary <string, string>)c_info.GetValueOrDefault("nicknames"), color: (string)c_info.GetValueOrDefault("color"), emoji: (JToken)c_info.GetValueOrDefault("emoji"), admins: new HashSet <string>(data.get("thread_admins")?.Select(node => node.get("id")?.Value <string>())), approval_mode: data.get("approval_mode")?.Value <bool>() ?? false, approval_requests: data.get("group_approval_queue") != null ? new HashSet <string>(data.get("group_approval_queue")?.get("nodes")?.Select(node => node.get("requester")?.get("id")?.Value <string>())) : null, photo: data.get("image")?.get("uri")?.Value <string>(), name: data.get("name")?.Value <string>(), message_count: data.get("messages_count")?.Value <int>() ?? 0, last_message_timestamp: last_message_timestamp, plan: plan)); }
/// <summary> /// Get thread list of your facebook account /// </summary> /// <param name="limit">Max.number of threads to retrieve. Capped at 20</param> /// <param name="thread_location">models.ThreadLocation: INBOX, PENDING, ARCHIVED or OTHER</param> /// <param name="before">A unix timestamp, indicating from which point to retrieve messages</param> public async Task <List <FB_Thread> > fetchThreadList(int limit = 20, string thread_location = ThreadLocation.INBOX, string before = null) { /* * Get thread list of your facebook account * :param limit: Max.number of threads to retrieve.Capped at 20 * :param thread_location: models.ThreadLocation: INBOX, PENDING, ARCHIVED or OTHER * :param before: A timestamp (in milliseconds), indicating from which point to retrieve threads * :type limit: int * :return: `models.Thread` objects * :rtype: list * :raises: Exception if request failed */ if (limit > 20 || limit < 1) { throw new FBchatUserError("`limit` should be between 1 and 20"); } var dict = new Dictionary <string, object>() { { "limit", limit }, { "tags", new string[] { thread_location } }, { "before", before }, { "includeDeliveryReceipts", true }, { "includeSeqID", false } }; var j = await this._session.graphql_request(GraphQL.from_doc_id(doc_id: "1349387578499440", param: dict)); var rtn = new List <FB_Thread>(); foreach (var node in j.get("viewer")?.get("message_threads")?.get("nodes")) { var _type = node.get("thread_type")?.Value <string>(); if (_type == "GROUP") { rtn.Add(FB_Group._from_graphql(_session, node)); } else if (_type == "ONE_TO_ONE") { rtn.Add(FB_User._from_thread_fetch(_session, node)); } else if (_type == "MARKETPLACE") { rtn.Add(FB_Marketplace._from_graphql(_session, node)); } else { throw new FBchatException(string.Format("Unknown thread type: {0}", _type)); } } return(rtn); }
internal static FB_TypingStatus _from_thread_typing(Session session, JToken data) { var author = new FB_User(session: session, uid: data?.get("sender_fbid")?.Value <string>()); var thread = new FB_Group(session: session, uid: data?.get("thread")?.Value <string>()); var status = data?.get("state")?.Value <int>() == 1; return(new FB_TypingStatus() { author = author, thread = thread, status = status }); }
/// <summary> /// Find and get a thread by its name /// </summary> /// <param name="name">Name of the thread</param> /// <param name="limit">The max. amount of threads to fetch</param> /// <returns>`FB_User`, `FB_Group` and `FB_Page` objects, ordered by relevance</returns> public async Task <List <FB_Thread> > searchThreads(string name, int limit = 1) { /* * Find and get a thread by its name * :param name: Name of the thread * :param limit: The max. amount of groups to fetch * : return: `User`, `Group` and `Page` objects, ordered by relevance * :rtype: list * :raises: FBchatException if request failed * */ var param = new Dictionary <string, object>() { { "search", name }, { "limit", limit.ToString() } }; var j = await this._session.graphql_request(GraphQL.from_query(GraphQL.SEARCH_THREAD, param)); List <FB_Thread> rtn = new List <FB_Thread>(); foreach (var node in j[name]?.get("threads")?.get("nodes")) { if (node.get("__typename").Value <string>().Equals("User")) { rtn.Add(FB_User._from_graphql(_session, node)); } else if (node.get("__typename").Value <string>().Equals("MessageThread")) { // MessageThread => Group thread rtn.Add(FB_Group._from_graphql(_session, node)); } else if (node.get("__typename").Value <string>().Equals("Page")) { rtn.Add(FB_Page._from_graphql(_session, node)); } else if (node.get("__typename").Value <string>().Equals("Group")) { // We don"t handle Facebook "Groups" continue; } else { Debug.WriteLine(string.Format("Unknown __typename: {0} in {1}", node.get("__typename").Value <string>(), node)); } } return(rtn); }
/// <summary> /// Find and get group thread by its name /// </summary> /// <param name="name">Name of the group</param> /// <param name="limit">The max. amount of groups to fetch</param> /// <returns>`FB_Group` objects, ordered by relevance</returns> public async Task <List <FB_Group> > searchGroups(string name, int limit = 1) { /* * Find and get group thread by its name * :param name: Name of the group thread * :param limit: The max. amount of groups to fetch * :return: `Group` objects, ordered by relevance * :rtype: list * :raises: FBchatException if request failed * */ var param = new Dictionary <string, object>() { { "search", name }, { "limit", limit.ToString() } }; var j = await this._session.graphql_request(GraphQL.from_query(GraphQL.SEARCH_GROUP, param)); return(j.get("viewer")?.get("groups")?.get("nodes").Select(node => FB_Group._from_graphql(_session, node)).ToList()); }
/// <summary> /// Get threads' info from IDs, unordered /// </summary> /// <param name="thread_ids">One or more thread ID(s) to query</param> /// <returns>A dictionary of FB_Thread objects, labeled by their ID</returns> public async Task <Dictionary <string, FB_Thread> > fetchThreadInfo(List <string> thread_ids) { /* * Get threads" info from IDs, unordered * ..warning:: * Sends two requests if users or pages are present, to fetch all available info! * :param thread_ids: One or more thread ID(s) to query * :return: `models.Thread` objects, labeled by their ID * :rtype: dict * :raises: Exception if request failed */ var queries = new List <GraphQL>(); foreach (var thread_id in thread_ids) { queries.Add(GraphQL.from_doc_id(doc_id: "2147762685294928", param: new Dictionary <string, object>() { { "id", thread_id }, { "message_limit", 0.ToString() }, { "load_messages", false.ToString() }, { "load_read_receipts", false.ToString() }, { "before", null } })); } var j = await this._session.graphql_requests(queries); foreach (var obj in j.Select((x, index) => new { entry = x, i = index })) { if (obj.entry.get("message_thread") == null) { // If you don't have an existing thread with this person, attempt to retrieve user data anyways j[obj.i]["message_thread"] = new JObject( new JProperty("thread_key", new JObject( new JProperty("other_user_id", thread_ids[obj.i]))), new JProperty("thread_type", "ONE_TO_ONE")); } } var pages_and_user_ids = j.Where(k => k.get("message_thread")?.get("thread_type")?.Value <string>()?.Equals("ONE_TO_ONE") ?? false) .Select(k => k.get("message_thread")?.get("thread_key")?.get("other_user_id")?.Value <string>()); JObject pages_and_users = null; if (pages_and_user_ids.Count() != 0) { pages_and_users = await this._fetchInfo(pages_and_user_ids.ToList()); } var rtn = new Dictionary <string, FB_Thread>(); foreach (var obj in j.Select((x, index) => new { entry = x, i = index })) { var entry = obj.entry.get("message_thread"); if (entry.get("thread_type")?.Value <string>()?.Equals("GROUP") ?? false) { var _id = entry.get("thread_key")?.get("thread_fbid").Value <string>(); rtn[_id] = FB_Group._from_graphql(_session, entry); } if (entry.get("thread_type")?.Value <string>()?.Equals("MARKETPLACE") ?? false) { var _id = entry.get("thread_key")?.get("thread_fbid").Value <string>(); rtn[_id] = FB_Marketplace._from_graphql(_session, entry); } else if (entry.get("thread_type")?.Value <string>()?.Equals("ONE_TO_ONE") ?? false) { var _id = entry.get("thread_key")?.get("other_user_id")?.Value <string>(); if (pages_and_users[_id] == null) { throw new FBchatException(string.Format("Could not fetch thread {0}", _id)); } foreach (var elem in pages_and_users[_id]) { entry[((JProperty)elem).Name] = ((JProperty)elem).Value; } if (entry.get("first_name") != null) { rtn[_id] = FB_User._from_graphql(_session, entry); } else { rtn[_id] = FB_Page._from_graphql(_session, entry); } } else { throw new FBchatException(string.Format("{0} had an unknown thread type: {1}", thread_ids[obj.i], entry)); } } return(rtn); }