Пример #1
0
        public static FB_QuickReply graphql_to_quick_reply(JToken q, bool is_response = false)
        {
            FB_QuickReply rtn   = null;
            var           data  = new Dictionary <string, object>();
            var           _type = q.get("content_type")?.Value <string>()?.ToLower();

            data["payload"] = q.get("payload");
            data["data"]    = q.get("data");
            if (q["image_url"] != null && _type != QuickReplyType.location.ToString())
            {
                data["image_url"] = q.get("image_url")?.Value <string>();
            }
            data["is_response"] = is_response;
            if (_type == QuickReplyType.text.ToString())
            {
                if (q.get("title") != null)
                {
                    data["title"] = q.get("title");
                }
                rtn = new FB_QuickReplyText(
                    (JToken)data.GetValueOrDefault("payload"),
                    (JToken)data.GetValueOrDefault("external_payload"),
                    (JToken)data.GetValueOrDefault("data"),
                    (bool)data.GetValueOrDefault("is_response"),
                    (string)data.GetValueOrDefault("title"),
                    (string)data.GetValueOrDefault("image_url"));
            }
            else if (_type == QuickReplyType.location.ToString())
            {
                rtn = new FB_QuickReplyLocation(
                    (JToken)data.GetValueOrDefault("payload"),
                    (JToken)data.GetValueOrDefault("external_payload"),
                    (JToken)data.GetValueOrDefault("data"),
                    (bool)data.GetValueOrDefault("is_response"));
            }
            else if (_type == QuickReplyType.user_phone_number.ToString())
            {
                rtn = new FB_QuickReplyPhoneNumber(
                    (JToken)data.GetValueOrDefault("payload"),
                    (JToken)data.GetValueOrDefault("external_payload"),
                    (JToken)data.GetValueOrDefault("data"),
                    (bool)data.GetValueOrDefault("is_response"),
                    (string)data.GetValueOrDefault("image_url"));
            }
            else if (_type == QuickReplyType.user_email.ToString())
            {
                rtn = new FB_QuickReplyEmail(
                    (JToken)data.GetValueOrDefault("payload"),
                    (JToken)data.GetValueOrDefault("external_payload"),
                    (JToken)data.GetValueOrDefault("data"),
                    (bool)data.GetValueOrDefault("is_response"),
                    (string)data.GetValueOrDefault("image_url"));
            }
            return(rtn);
        }
Пример #2
0
 /// <summary>
 /// Replies to a chosen quick reply
 /// </summary>
 /// <param name="quick_reply">Quick reply to reply to</param>
 /// <param name="payload">Optional answer to the quick reply</param>
 /// <returns></returns>
 public async Task <string> quickReply(FB_QuickReply quick_reply, dynamic payload = null)
 {
     /*
      * Replies to a chosen quick reply
      * :param quick_reply: Quick reply to reply to
      * :param payload: Optional answer to the quick reply
      * :type quick_reply: QuickReply
      * :return: :ref:`Message ID<intro_message_ids>` of the sent message
      * :raises: FBchatException if request failed
      * */
     quick_reply.is_response = true;
     if (quick_reply is FB_QuickReplyText)
     {
         return(await this.send(
                    new FB_Message(session : this.session, text : ((FB_QuickReplyText)quick_reply).title, quick_replies : new List <FB_QuickReply>()
         {
             quick_reply
         })
                    ));
     }
     else if (quick_reply is FB_QuickReplyLocation)
     {
         if (!(payload is FB_LocationAttachment))
         {
             throw new ArgumentException(
                       "Payload must be an instance of `fbchat-sharp.LocationAttachment`"
                       );
         }
         return(await this.sendLocation(payload));
     }
     else if (quick_reply is FB_QuickReplyEmail)
     {
         //if (payload == null)
         //    payload = (await this.getEmails())[0];
         quick_reply.external_payload = quick_reply.payload;
         quick_reply.payload          = payload;
         return(await this.send(new FB_Message(session : this.session, text : payload, quick_replies : new List <FB_QuickReply>()
         {
             quick_reply
         })));
     }
     else if (quick_reply is FB_QuickReplyPhoneNumber)
     {
         //if (payload == null)
         //    payload = (await this.getPhoneNumbers())[0];
         quick_reply.external_payload = quick_reply.payload;
         quick_reply.payload          = payload;
         return(await this.send(new FB_Message(session : this.session, text : payload, quick_replies : new List <FB_QuickReply>()
         {
             quick_reply
         })));
     }
     return(null);
 }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #5
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);
        }