From e579e0c76741bf087eff0ed2b75ad73d9871ec09 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 10 Mar 2019 19:35:07 +0100 Subject: [PATCH] Move graphql_to_quick_reply into _quick_reply.py --- fbchat/_graphql.py | 31 +++++-------------------------- fbchat/_quick_reply.py | 23 +++++++++++++++++++++++ fbchat/graphql.py | 1 - 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index 370be44..672558c 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, _message +from . import _file, _message, _quick_reply from .models import * from ._util import * @@ -28,29 +28,6 @@ class ConcatJSONDecoder(json.JSONDecoder): # End shameless copy -def graphql_to_quick_reply(q, is_response=False): - data = dict() - _type = q.get("content_type").lower() - if q.get("payload"): - data["payload"] = q["payload"] - if q.get("data"): - data["data"] = q["data"] - if q.get("image_url") and _type is not QuickReplyLocation._type: - data["image_url"] = q["image_url"] - data["is_response"] = is_response - if _type == QuickReplyText._type: - if q.get("title") is not None: - data["title"] = q["title"] - rtn = QuickReplyText(**data) - elif _type == QuickReplyLocation._type: - rtn = QuickReplyLocation(**data) - elif _type == QuickReplyPhoneNumber._type: - rtn = QuickReplyPhoneNumber(**data) - elif _type == QuickReplyEmail._type: - rtn = QuickReplyEmail(**data) - return rtn - - def graphql_to_message(message): if message.get("message_sender") is None: message["message_sender"] = {} @@ -87,10 +64,12 @@ def graphql_to_message(message): if message.get("platform_xmd_encoded"): quick_replies = json.loads(message["platform_xmd_encoded"]).get("quick_replies") if isinstance(quick_replies, list): - rtn.quick_replies = [graphql_to_quick_reply(q) for q in quick_replies] + rtn.quick_replies = [ + _quick_reply.graphql_to_quick_reply(q) for q in quick_replies + ] elif isinstance(quick_replies, dict): rtn.quick_replies = [ - graphql_to_quick_reply(quick_replies, is_response=True) + _quick_reply.graphql_to_quick_reply(quick_replies, is_response=True) ] if message.get("extensible_attachment") is not None: attachment = _message.graphql_to_extensible_attachment( diff --git a/fbchat/_quick_reply.py b/fbchat/_quick_reply.py index 60323bf..02c7f89 100644 --- a/fbchat/_quick_reply.py +++ b/fbchat/_quick_reply.py @@ -74,3 +74,26 @@ class QuickReplyEmail(QuickReply): def __init__(self, image_url=None, **kwargs): super(QuickReplyEmail, self).__init__(**kwargs) self.image_url = image_url + + +def graphql_to_quick_reply(q, is_response=False): + data = dict() + _type = q.get("content_type").lower() + if q.get("payload"): + data["payload"] = q["payload"] + if q.get("data"): + data["data"] = q["data"] + if q.get("image_url") and _type is not QuickReplyLocation._type: + data["image_url"] = q["image_url"] + data["is_response"] = is_response + if _type == QuickReplyText._type: + if q.get("title") is not None: + data["title"] = q["title"] + rtn = QuickReplyText(**data) + elif _type == QuickReplyLocation._type: + rtn = QuickReplyLocation(**data) + elif _type == QuickReplyPhoneNumber._type: + rtn = QuickReplyPhoneNumber(**data) + elif _type == QuickReplyEmail._type: + rtn = QuickReplyEmail(**data) + return rtn diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 412c42f..f0ea04f 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -8,7 +8,6 @@ from ._graphql import ( FLAGS, WHITESPACE, ConcatJSONDecoder, - graphql_to_quick_reply, graphql_to_message, graphql_to_thread, graphql_queries_to_json,