예제 #1
0
 /// <summary>
 /// Deletes specifed messages
 /// </summary>
 /// <param name="message_ids">Message IDs to delete</param>
 public async Task deleteMessages(List <string> message_ids)
 {
     /*
      * Deletes specifed messages
      * :param message_ids: Message IDs to delete
      * :return: true
      * :raises: FBchatException if request failed
      * */
     await FB_Message._delete_many(_session, message_ids);
 }
예제 #2
0
        internal static FB_UnfetchedThreadEvent _parse(Session session, JToken data)
        {
            var thread  = FB_ThreadEvent._get_thread(session, data);
            var message = new FB_Message(session, thread_id: thread.uid, uid: data?.get("messageId")?.Value <string>());

            return(new FB_UnfetchedThreadEvent()
            {
                thread = thread,
                message = message
            });
        }
예제 #3
0
        /// TODO: This!

        internal static FB_LiveLocationEvent _parse(Session session, JToken data)
        {
            var thread = FB_LiveLocationEvent._get_thread(session, data);

            foreach (var location_data in data?.get("messageLiveLocations") ?? Enumerable.Empty <JToken>())
            {
                var message  = new FB_Message(session: session, thread_id: thread.uid, uid: data?.get("messageId")?.Value <string>());
                var author   = new FB_User(session: session, uid: location_data?.get("senderId")?.Value <string>());
                var location = FB_LiveLocationAttachment._from_pull(location_data);
            }

            return(null);
        }
예제 #4
0
        internal static FB_MessageReplyEvent _parse(Session session, JToken data)
        {
            var thread   = FB_MessageReplyEvent._get_thread(session, data);
            var metadata = data?.get("message")?.get("messageMetadata");

            return(new FB_MessageReplyEvent()
            {
                author = new FB_User(session: session, uid: metadata?.get("actorFbId")?.Value <string>()),
                thread = thread,
                message = FB_Message._from_reply(data?.get("message"), thread),
                replied_to = FB_Message._from_reply(data?.get("repliedToMessage"), thread),
            });
        }
예제 #5
0
        internal static FB_MessageEvent _parse(Session session, JToken data)
        {
            (FB_User author, FB_Thread thread, long at) = FB_MessageEvent._parse_metadata(session, data);
            var message = FB_Message._from_pull(data, thread: thread, author: author.uid, timestamp: at.ToString());

            return(new FB_MessageEvent()
            {
                author = author,
                thread = thread,
                message = message,
                at = at
            });
        }
예제 #6
0
        /// <summary>
        /// Fetches`Message` object from the message id
        /// </summary>
        /// <param name="thread">Thread containing this message</param>
        /// <param name="mid">Message ID to fetch from</param>
        /// <returns>`FB_Message` object</returns>
        public static async Task <FB_Message> _from_fetch(FB_Thread thread, string mid)
        {
            /*
             * Fetches`Message` object from the message id
             * :param thread: Thread containing this message
             * :param mid: Message ID to fetch from
             * :return: `Message` object
             * :rtype: Message
             * :raises: FBchatException if request failed
             * */
            var message_info = ((JToken)await thread._forcedFetch(mid))?.get("message");

            return(FB_Message._from_graphql(message_info, thread));
        }
예제 #7
0
        /// <summary>
        /// Sends a message to a thread
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <returns>Message ID of the sent message</returns>
        public async Task <string> send(FB_Message message = null)
        {
            /*
             * Sends a message to a thread
             * :param message: Message to send
             * :type message: models.Message
             * :return: :ref:`Message ID <intro_message_ids>` of the sent message
             * :raises: FBchatException if request failed
             */

            var data = this._to_send_data();

            data.update(message._to_send_data());
            return(await this.session._do_send_request(data));
        }
예제 #8
0
        private async Task <dynamic> _sendLocation(
            FB_LocationAttachment location, bool current = true, FB_Message message = null
            )
        {
            var data = this._to_send_data();

            if (message != null)
            {
                data.update(message._to_send_data());
            }
            data["action_type"] = "ma-type:user-generated-message";
            data["location_attachment[coordinates][latitude]"]  = location.latitude;
            data["location_attachment[coordinates][longitude]"] = location.longitude;
            data["location_attachment[is_current_location]"]    = current;
            return(await this.session._do_send_request(data));
        }
예제 #9
0
 /// <summary>
 /// Sends a given location to a thread as the user's current location
 /// </summary>
 /// <param name="location">Location to send</param>
 /// <param name="message">Additional message</param>
 /// <returns>:ref:`Message ID` of the sent message</returns>
 public async Task <string> sendLocation(FB_LocationAttachment location, FB_Message message = null)
 {
     /*
      * Sends a given location to a thread as the user's current location
      * :param location: Location to send
      * :param message: Additional message
      * :type location: LocationAttachment
      * :type message: Message
      * :return: :ref:`Message ID<intro_message_ids>` of the sent message
      * :raises: FBchatException if request failed
      * */
     return(await this._sendLocation(
                location : location,
                current : true,
                message : message
                ));
 }
예제 #10
0
        public static FB_Message graphql_to_message(string thread_id, JToken message)
        {
            if (message["message_sender"] == null || message["message_sender"].Type == JTokenType.Null)
            {
                message["message_sender"] = new JObject(new JProperty("id", 0));
            }
            if (message["message"] == null || message["message"].Type == JTokenType.Null)
            {
                message["message"] = new JObject(new JProperty("text", ""));
            }

            var rtn = new FB_Message(
                text: message["message"]["text"]?.Value <string>(),
                mentions: new List <FB_Mention>(),
                sticker: graphql_to_sticker(message["sticker"]));

            rtn.thread_id = thread_id;
            rtn.uid       = message["message_id"]?.Value <string>();
            rtn.author    = message["message_sender"]["id"]?.Value <string>();
            rtn.timestamp = message["timestamp_precise"]?.Value <string>();
            if (message["unread"] != null)
            {
                rtn.is_read = !message["unread"].Value <bool>();
            }
            rtn.reactions = new Dictionary <string, MessageReaction>();

            foreach (var r in message["message_reactions"])
            {
                rtn.reactions.Add(r["user"]["id"]?.Value <string>(), Constants.REACTIONS[r["reaction"].Value <string>()]);
            }

            if (message["blob_attachments"] != null)
            {
                rtn.attachments = new List <FB_Attachment>();
                foreach (var attachment in message["blob_attachments"])
                {
                    rtn.attachments.Add(graphql_to_attachment(attachment));
                }
            }
            // TODO: This is still missing parsing:
            // message.get('extensible_attachment')
            return(rtn);
        }
예제 #11
0
        public static FB_Message _from_pull(JToken data, string thread_id, string mid = null, List <string> tags = null, string author = null, string timestamp = null)
        {
            var rtn = new FB_Message(
                text: data.get("body")?.Value <string>());

            rtn.uid       = mid;
            rtn.thread_id = thread_id; // Added
            rtn.author    = author;
            rtn.timestamp = timestamp;

            rtn.mentions = JToken.Parse(data.get("data")?.get("prng")?.Value <string>() ?? "{}")?.Select((m) =>
                                                                                                         new FB_Mention(
                                                                                                             thread_id: m.get("i")?.Value <string>(),
                                                                                                             offset: data.get("o")?.Value <int>() ?? 0,
                                                                                                             length: data.get("l")?.Value <int>() ?? 0)
                                                                                                         ).ToList();

            if (data.get("attachments") != null)
            {
                try
                {
                    foreach (var a in data.get("attachments"))
                    {
                        var mercury = a.get("mercury");
                        if (mercury.get("blob_attachment") != null)
                        {
                            var image_metadata = a.get("imageMetadata");
                            var attach_type    = mercury.get("blob_attachment")?.get("__typename")?.Value <string>();
                            var attachment     = FB_Attachment.graphql_to_attachment(
                                mercury.get("blob_attachment")
                                );

                            if (new string[] { "MessageFile", "MessageVideo", "MessageAudio" }.Contains(attach_type))
                            {
                                // TODO: Add more data here for audio files
                                if (attachment is FB_FileAttachment)
                                {
                                    ((FB_FileAttachment)attachment).size = a?.get("fileSize")?.Value <int>() ?? 0;
                                }
                                if (attachment is FB_VideoAttachment)
                                {
                                    ((FB_VideoAttachment)attachment).size = a?.get("fileSize")?.Value <int>() ?? 0;
                                }
                            }
                            rtn.attachments.Add(attachment);
                        }
                        else if (mercury.get("sticker_attachment") != null)
                        {
                            rtn.sticker = FB_Sticker._from_graphql(
                                mercury.get("sticker_attachment")
                                );
                        }
                        else if (mercury.get("extensible_attachment") != null)
                        {
                            var attachment = FB_Attachment.graphql_to_extensible_attachment(
                                mercury.get("extensible_attachment")
                                );
                            if (attachment is FB_UnsentMessage)
                            {
                                rtn.unsent = true;
                            }
                            else if (attachment != null)
                            {
                                rtn.attachments.Add(attachment);
                            }
                        }
                    }
                }
                catch
                {
                    Debug.WriteLine(string.Format("An exception occured while reading attachments: {0}", data.get("attachments")));
                }
            }

            rtn.emoji_size = EmojiSizeMethods._from_tags(tags);
            rtn.forwarded  = FB_Message._get_forwarded_from_tags(tags);
            return(rtn);
        }
예제 #12
0
        public static FB_Message _from_reply(JToken data, string thread_id)
        {
            var tags = data.get("messageMetadata")?.get("tags")?.ToObject <List <string> >();

            var rtn = new FB_Message(
                text: data.get("body")?.Value <string>(),
                mentions: JToken.Parse(data.get("data")?.get("prng")?.Value <string>() ?? "{}")?.Select((m) =>
                                                                                                        new FB_Mention(
                                                                                                            thread_id: m.get("i")?.Value <string>(),
                                                                                                            offset: data.get("o")?.Value <int>() ?? 0,
                                                                                                            length: data.get("l")?.Value <int>() ?? 0)
                                                                                                        ).ToList(),
                emoji_size: EmojiSizeMethods._from_tags(tags));

            var metadata = data.get("messageMetadata");

            rtn.forwarded = FB_Message._get_forwarded_from_tags(tags);
            rtn.uid       = metadata?.get("messageId")?.Value <string>();
            rtn.thread_id = thread_id; // Added
            rtn.author    = metadata?.get("actorFbId")?.Value <string>();
            rtn.timestamp = metadata?.get("timestamp")?.Value <string>();
            rtn.unsent    = false;

            if (data.get("data")?.get("platform_xmd") != null)
            {
                var quick_replies = JToken.Parse(data.get("data")?.get("platform_xmd").Value <string>()).get("quick_replies");
                if (quick_replies.Type == JTokenType.Array)
                {
                    rtn.quick_replies = quick_replies.Select((q) => FB_QuickReply.graphql_to_quick_reply(q)).ToList();
                }
                else
                {
                    rtn.quick_replies = new List <FB_QuickReply>()
                    {
                        FB_QuickReply.graphql_to_quick_reply(quick_replies)
                    }
                };
            }
            if (data.get("attachments") != null)
            {
                foreach (var atc in data.get("attachments"))
                {
                    var attachment = JToken.Parse(atc.get("mercuryJSON")?.Value <string>());
                    if (attachment.get("blob_attachment") != null)
                    {
                        rtn.attachments.Add(
                            FB_Attachment.graphql_to_attachment(attachment.get("blob_attachment"))
                            );
                    }
                    if (attachment.get("extensible_attachment") != null)
                    {
                        var ext_attachment = FB_Attachment.graphql_to_extensible_attachment(attachment.get("extensible_attachment"));
                        if (ext_attachment is FB_UnsentMessage)
                        {
                            rtn.unsent = true;
                        }
                        else if (ext_attachment != null)
                        {
                            rtn.attachments.Add(ext_attachment);
                        }
                    }
                }
            }

            return(rtn);
        }
예제 #13
0
        public static FB_Message _from_graphql(JToken data, string thread_id)
        {
            if (data["message_sender"] == null)
            {
                data["message_sender"] = new JObject(new JProperty("id", 0));
            }
            if (data["message"] == null)
            {
                data["message"] = new JObject(new JProperty("text", ""));
            }

            var tags = data.get("tags_list")?.ToObject <List <string> >();

            var rtn = new FB_Message(
                text: data.get("message")?.get("text")?.Value <string>(),
                mentions: data.get("message")?.get("ranges")?.Select((m) =>
                                                                     new FB_Mention(
                                                                         thread_id: m.get("entity")?.get("id")?.Value <string>(),
                                                                         offset: data.get("offset")?.Value <int>() ?? 0,
                                                                         length: data.get("length")?.Value <int>() ?? 0)
                                                                     ).ToList(),
                emoji_size: EmojiSizeMethods._from_tags(tags),
                sticker: FB_Sticker._from_graphql(data.get("sticker")));

            rtn.forwarded = FB_Message._get_forwarded_from_tags(tags);
            rtn.uid       = data.get("message_id")?.Value <string>();
            rtn.thread_id = thread_id; // Added
            rtn.author    = data.get("message_sender")?.get("id")?.Value <string>();
            rtn.timestamp = data.get("timestamp_precise")?.Value <string>();
            rtn.unsent    = false;

            if (data.get("unread") != null)
            {
                rtn.is_read = !data.get("unread").Value <bool>();
            }
            rtn.reactions = new Dictionary <string, MessageReaction>();
            foreach (var r in data.get("message_reactions"))
            {
                rtn.reactions.Add(r.get("user")?.get("id")?.Value <string>(), FB_Message_Constants.REACTIONS[r.get("reaction").Value <string>()]);
            }
            if (data.get("blob_attachments") != null)
            {
                rtn.attachments = new List <FB_Attachment>();
                foreach (var attachment in data.get("blob_attachments"))
                {
                    rtn.attachments.Add(FB_Attachment.graphql_to_attachment(attachment));
                }
            }
            if (data.get("platform_xmd_encoded") != null)
            {
                var quick_replies = JToken.Parse(data.get("platform_xmd_encoded")?.Value <string>()).get("quick_replies");
                if (quick_replies != null)
                {
                    if (quick_replies.Type == JTokenType.Array)
                    {
                        rtn.quick_replies = quick_replies.Select((q) => FB_QuickReply.graphql_to_quick_reply(q)).ToList();
                    }
                    else
                    {
                        rtn.quick_replies = new List <FB_QuickReply>()
                        {
                            FB_QuickReply.graphql_to_quick_reply(quick_replies)
                        }
                    };
                }
            }
            if (data.get("extensible_attachment") != null)
            {
                var attachment = FB_Attachment.graphql_to_extensible_attachment(data.get("extensible_attachment"));
                if (attachment is FB_UnsentMessage)
                {
                    rtn.unsent = true;
                }
                else if (attachment != null)
                {
                    rtn.attachments.Add(attachment);
                }
            }
            if (data.get("replied_to_message") != null)
            {
                rtn.replied_to  = FB_Message._from_graphql(data.get("replied_to_message")?.get("message"), thread_id);
                rtn.reply_to_id = rtn.replied_to.uid;
            }

            return(rtn);
        }
예제 #14
0
 /// <summary>
 /// Facebook messenger message class
 /// </summary>
 /// <param name="text"></param>
 /// <param name="mentions"></param>
 /// <param name="emoji_size"></param>
 /// <param name="uid"></param>
 /// <param name="author"></param>
 /// <param name="timestamp"></param>
 /// <param name="is_read"></param>
 /// <param name="read_by"></param>
 /// <param name="reactions"></param>
 /// <param name="sticker"></param>
 /// <param name="attachments"></param>
 /// <param name="quick_replies"></param>
 /// <param name="unsent"></param>
 /// <param name="reply_to_id"></param>
 /// <param name="replied_to"></param>
 /// <param name="forwarded"></param>
 /// <param name="is_from_me"></param>
 /// <param name="thread_id"></param>
 public FB_Message(string text = null, List <FB_Mention> mentions = null, EmojiSize?emoji_size = null, string uid = null, string author = null, string timestamp = null, bool is_read = false, List <string> read_by = null, Dictionary <string, MessageReaction> reactions = null, FB_Sticker sticker = null, List <FB_Attachment> attachments = null, List <FB_QuickReply> quick_replies = null, bool unsent = false, string reply_to_id = null, FB_Message replied_to = null, bool forwarded = false, bool is_from_me = false, string thread_id = null)
 {
     this.text          = text;
     this.mentions      = mentions ?? new List <FB_Mention>();
     this.emoji_size    = emoji_size;
     this.uid           = uid;
     this.author        = author;
     this.timestamp     = timestamp;
     this.is_read       = is_read;
     this.read_by       = read_by ?? new List <string>();
     this.reactions     = reactions ?? new Dictionary <string, MessageReaction>();
     this.sticker       = sticker;
     this.attachments   = attachments ?? new List <FB_Attachment>();
     this.quick_replies = quick_replies ?? new List <FB_QuickReply>();
     this.unsent        = unsent;
     this.reply_to_id   = reply_to_id;
     this.replied_to    = replied_to;
     this.forwarded     = forwarded;
     this.is_from_me    = is_from_me;
     this.thread_id     = thread_id;
 }
예제 #15
0
        internal static FB_Message _from_graphql(JToken data, FB_Thread thread, JToken read_receipts = null)
        {
            if (data["message_sender"] == null)
            {
                data["message_sender"] = new JObject(new JProperty("id", 0));
            }
            if (data["message"] == null)
            {
                data["message"] = new JObject(new JProperty("text", ""));
            }

            var tags = data.get("tags_list")?.ToObject <List <string> >();

            var rtn = new FB_Message(
                session: thread.session,
                text: data.get("message")?.get("text")?.Value <string>(),
                mentions: data.get("message")?.get("ranges")?.Select((m) =>
                                                                     FB_Mention._from_range(m)
                                                                     ).ToList(),
                emoji_size: EmojiSizeMethods._from_tags(tags),
                sticker: FB_Sticker._from_graphql(data.get("sticker")));

            rtn.forwarded = FB_Message._get_forwarded_from_tags(tags);
            rtn.uid       = data.get("message_id")?.Value <string>();
            rtn.thread_id = thread.uid; // Added
            rtn.author    = data.get("message_sender")?.get("id")?.Value <string>();
            rtn.timestamp = data.get("timestamp_precise")?.Value <string>();
            rtn.unsent    = false;

            if (data.get("unread") != null)
            {
                rtn.is_read = !data.get("unread").Value <bool>();
            }

            rtn.reactions = new Dictionary <string, string>();
            foreach (var r in data.get("message_reactions"))
            {
                rtn.reactions.Add(r.get("user")?.get("id")?.Value <string>(), r.get("reaction").Value <string>());
            }
            if (data.get("blob_attachments") != null)
            {
                rtn.attachments = new List <FB_Attachment>();
                foreach (var attachment in data.get("blob_attachments"))
                {
                    rtn.attachments.Add(FB_Attachment.graphql_to_attachment(attachment));
                }
            }
            if (data.get("platform_xmd_encoded") != null)
            {
                var quick_replies = JToken.Parse(data.get("platform_xmd_encoded")?.Value <string>()).get("quick_replies");
                if (quick_replies != null)
                {
                    if (quick_replies.Type == JTokenType.Array)
                    {
                        rtn.quick_replies = quick_replies.Select((q) => FB_QuickReply.graphql_to_quick_reply(q)).ToList();
                    }
                    else
                    {
                        rtn.quick_replies = new List <FB_QuickReply>()
                        {
                            FB_QuickReply.graphql_to_quick_reply(quick_replies)
                        }
                    };
                }
            }
            if (data.get("extensible_attachment") != null)
            {
                var attachment = FB_Attachment.graphql_to_extensible_attachment(data.get("extensible_attachment"));
                if (attachment is FB_UnsentMessage)
                {
                    rtn.unsent = true;
                }
                else if (attachment != null)
                {
                    rtn.attachments.Add(attachment);
                }
            }
            if (data.get("replied_to_message") != null && data.get("replied_to_message").get("message") != null)
            {
                // data["replied_to_message"]["message"] is None if the message is deleted
                rtn.replied_to  = FB_Message._from_graphql(data.get("replied_to_message").get("message"), thread);
                rtn.reply_to_id = rtn.replied_to.uid;
            }
            if (read_receipts != null)
            {
                rtn.read_by = read_receipts.Where(r => long.Parse(r.get("watermark")?.Value <string>()) >= long.Parse(rtn.timestamp))
                              .Select(r => r.get("actor")?.get("id")?.Value <string>()).ToList();
            }
            return(rtn);
        }