Better quick reply types

This commit is contained in:
Kacper Ziubryniewicz
2019-01-05 20:06:28 +01:00
parent 4b485d54b6
commit 7f6843df55
3 changed files with 27 additions and 26 deletions

View File

@@ -1068,11 +1068,13 @@ class Client(object):
xmd = {"quick_replies": []}
for quick_reply in message.quick_replies:
q = dict()
q["content_type"] = quick_reply.type.value.lower()
q["content_type"] = quick_reply._type
q["payload"] = quick_reply.payload
q["external_payload"] = quick_reply.external_payload
q["data"] = quick_reply.data
if quick_reply.type == QuickReplyType.TEXT: q["title"] = quick_reply.title
if quick_reply.type is not QuickReplyType.LOCATION: q["image_url"] = quick_reply.image_url
if quick_reply.is_response: q["ignore_for_webhook"] = False
if quick_reply._type == QuickReplyText._type: q["title"] = quick_reply.title
if quick_reply._type is not QuickReplyLocation._type: q["image_url"] = quick_reply.image_url
xmd["quick_replies"].append(q)
if len(message.quick_replies) == 1 and message.quick_replies[0].is_response:
xmd["quick_replies"] = xmd["quick_replies"][0]
@@ -1163,14 +1165,14 @@ class Client(object):
:raises: FBchatException if request failed
"""
quick_reply.is_response = True
if quick_reply.type == QuickReplyType.TEXT:
if isinstance(quick_reply, QuickReplyText):
return self.send(Message(text=quick_reply.title, quick_replies=[quick_reply]))
elif quick_reply.type == QuickReplyType.EMAIL:
elif isinstance(quick_reply, QuickReplyEmail):
if not payload: payload = self.getEmails()[0]
quick_reply.external_payload = quick_reply.payload
quick_reply.payload = payload
return self.send(Message(text=payload, quick_replies=[quick_reply]))
elif quick_reply.type == QuickReplyType.PHONE_NUMBER:
elif isinstance(quick_reply, QuickReplyPhoneNumber):
if not payload: payload = self.getPhoneNumbers()[0]
quick_reply.external_payload = quick_reply.payload
quick_reply.payload = payload

View File

@@ -270,19 +270,19 @@ def graphql_to_plan(a):
def graphql_to_quick_reply(q, is_response=False):
data = dict()
_type = QuickReplyType(q.get('content_type').upper())
_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 QuickReplyType.LOCATION: data["image_url"] = q["image_url"]
if q.get('image_url') and _type is not QuickReplyLocation._type: data["image_url"] = q["image_url"]
data["is_response"] = is_response
if _type == QuickReplyType.TEXT:
if _type == QuickReplyText._type:
if q.get('title') is not None: data["title"] = q["title"]
rtn = QuickReplyText(**data)
elif _type == QuickReplyType.LOCATION:
elif _type == QuickReplyLocation._type:
rtn = QuickReplyLocation(**data)
elif _type == QuickReplyType.PHONE_NUMBER:
elif _type == QuickReplyPhoneNumber._type:
rtn = QuickReplyPhoneNumber(**data)
elif _type == QuickReplyType.EMAIL:
elif _type == QuickReplyEmail._type:
rtn = QuickReplyEmail(**data)
return rtn

View File

@@ -527,8 +527,6 @@ class Mention(object):
return '<Mention {}: offset={} length={}>'.format(self.thread_id, self.offset, self.length)
class QuickReply(object):
#: Type of the quick reply
type = None
#: Payload of the quick reply
payload = None
#: External payload for responses
@@ -538,9 +536,8 @@ class QuickReply(object):
#: Whether it's a response for a quick reply
is_response = None
def __init__(self, _type=None, payload=None, data=None, is_response=False):
def __init__(self, payload=None, data=None, is_response=False):
"""Represents a quick reply"""
self.type = _type
self.payload = payload
self.data = data
self.is_response = is_response
@@ -556,36 +553,44 @@ class QuickReplyText(QuickReply):
title = None
#: URL of the quick reply image (optional)
image_url = None
#: Type of the quick reply
_type = "text"
def __init__(self, title=None, image_url=None, **kwargs):
"""Represents a text quick reply"""
super(QuickReplyText, self).__init__(_type=QuickReplyType.TEXT, **kwargs)
super(QuickReplyText, self).__init__(**kwargs)
self.title = title
self.image_url = image_url
class QuickReplyLocation(QuickReply):
#: Type of the quick reply
_type = "location"
def __init__(self, **kwargs):
"""Represents a location quick reply (Doesn't work on mobile)"""
super(QuickReplyLocation, self).__init__(_type=QuickReplyType.LOCATION, **kwargs)
super(QuickReplyLocation, self).__init__(**kwargs)
self.is_response = False
class QuickReplyPhoneNumber(QuickReply):
#: URL of the quick reply image (optional)
image_url = None
#: Type of the quick reply
_type = "user_phone_number"
def __init__(self, image_url=None, **kwargs):
"""Represents a phone number quick reply (Doesn't work on mobile)"""
super(QuickReplyPhoneNumber, self).__init__(_type=QuickReplyType.PHONE_NUMBER, **kwargs)
super(QuickReplyPhoneNumber, self).__init__(**kwargs)
self.image_url = image_url
class QuickReplyEmail(QuickReply):
#: URL of the quick reply image (optional)
image_url = None
#: Type of the quick reply
_type = "user_email"
def __init__(self, image_url=None, **kwargs):
"""Represents an email quick reply (Doesn't work on mobile)"""
super(QuickReplyEmail, self).__init__(_type=QuickReplyType.EMAIL, **kwargs)
super(QuickReplyEmail, self).__init__(**kwargs)
self.image_url = image_url
class Poll(object):
@@ -675,12 +680,6 @@ class Enum(enum.Enum):
# For documentation:
return '{}.{}'.format(type(self).__name__, self.name)
class QuickReplyType(Enum):
TEXT = 'TEXT'
LOCATION = 'LOCATION'
PHONE_NUMBER = 'USER_PHONE_NUMBER'
EMAIL = 'USER_EMAIL'
class ThreadType(Enum):
"""Used to specify what type of Facebook thread is being used. See :ref:`intro_threads` for more info"""
USER = 1