diff --git a/fbchat/_client.py b/fbchat/_client.py index 168392e..9ff9c47 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -8,7 +8,7 @@ from random import choice from bs4 import BeautifulSoup as bs from mimetypes import guess_type from collections import OrderedDict -from . import _file +from . import _file, _message from ._util import * from .models import * from .graphql import * @@ -3077,7 +3077,7 @@ class Client(object): ) elif mercury.get("extensible_attachment"): - attachment = graphql_to_extensible_attachment( + attachment = _message.graphql_to_extensible_attachment( mercury["extensible_attachment"] ) if isinstance(attachment, UnsentMessage): diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index 1004f68..370be44 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import json import re -from . import _file +from . import _file, _message from .models import * from ._util import * @@ -28,26 +28,6 @@ class ConcatJSONDecoder(json.JSONDecoder): # End shameless copy -def graphql_to_extensible_attachment(a): - story = a.get("story_attachment") - if not story: - return None - - target = story.get("target") - if not target: - return UnsentMessage(uid=a.get("legacy_attachment_id")) - - _type = target["__typename"] - if _type == "MessageLocation": - return LocationAttachment._from_graphql(story) - elif _type == "MessageLiveLocation": - return LiveLocationAttachment._from_graphql(story) - elif _type in ["ExternalUrl", "Story"]: - return ShareAttachment._from_graphql(story) - - return None - - def graphql_to_quick_reply(q, is_response=False): data = dict() _type = q.get("content_type").lower() @@ -113,7 +93,9 @@ def graphql_to_message(message): graphql_to_quick_reply(quick_replies, is_response=True) ] if message.get("extensible_attachment") is not None: - attachment = graphql_to_extensible_attachment(message["extensible_attachment"]) + attachment = _message.graphql_to_extensible_attachment( + message["extensible_attachment"] + ) if isinstance(attachment, UnsentMessage): rtn.unsent = True elif attachment: diff --git a/fbchat/_message.py b/fbchat/_message.py index 59e52ba..3ba4c10 100644 --- a/fbchat/_message.py +++ b/fbchat/_message.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import attr from string import Formatter +from . import _attachment, _location from ._core import Enum @@ -137,3 +138,23 @@ class Message(object): message = cls(text=result, mentions=mentions) return message + + +def graphql_to_extensible_attachment(data): + story = data.get("story_attachment") + if not story: + return None + + target = story.get("target") + if not target: + return _attachment.UnsentMessage(uid=data.get("legacy_attachment_id")) + + _type = target["__typename"] + if _type == "MessageLocation": + return _location.LocationAttachment._from_graphql(story) + elif _type == "MessageLiveLocation": + return _location.LiveLocationAttachment._from_graphql(story) + elif _type in ["ExternalUrl", "Story"]: + return _attachment.ShareAttachment._from_graphql(story) + + return None diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 06e416e..412c42f 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -8,7 +8,6 @@ from ._graphql import ( FLAGS, WHITESPACE, ConcatJSONDecoder, - graphql_to_extensible_attachment, graphql_to_quick_reply, graphql_to_message, graphql_to_thread,