Skip to content

Commit

Permalink
#4, support notification type
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwz committed Aug 26, 2016
1 parent f344ebe commit fc08dfe
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Latest
* Fix, trigger message_handler, postback_handler even if custom callbacks are used
* Feature : Add a callback function in page.send
* Feature : Add a page option 'after_send' (support decorator @page.after_send)
* Feature : Support notification type (e.g page.send(notification_type=NotificationType.SILENT_PUSH))

1.6.0
-----
Expand Down
4 changes: 2 additions & 2 deletions example/messenger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from config import CONFIG
from fbmq import Attachment, Template, QuickReply
from fbmq import Attachment, Template, QuickReply, NotificationType
from fbpage import page

USER_SEQ = {}
Expand Down Expand Up @@ -136,7 +136,7 @@ def send_message(recipient_id, text):
if text in special_keywords:
special_keywords[text](recipient_id)
else:
page.send(recipient_id, text, callback=send_text_callback)
page.send(recipient_id, text, callback=send_text_callback, notification_type=NotificationType.REGULAR)


def send_text_callback(payload, response):
Expand Down
4 changes: 2 additions & 2 deletions fbmq/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__version__ = '1.7.0b4'
__version__ = '1.7.0b5'

from .fbmq import QuickReply, Page
from .fbmq import *
from . import attachment as Attachment
from . import template as Template
25 changes: 20 additions & 5 deletions fbmq/fbmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
from .payload import *


# I agree with him : http://stackoverflow.com/a/36937/3843242
class NotificationType:
REGULAR = 'REGULAR'
SILENT_PUSH = 'SILENT_PUSH'
NO_PUSH = 'NO_PUSH'


class SenderAction:
TYPING_ON='typing_on'
TYPING_OFF='typing_off'
MARK_SEEN='mark_seen'


class Page(object):
def __init__(self, page_access_token, **options):
self.page_access_token = page_access_token
Expand Down Expand Up @@ -113,33 +126,35 @@ def _send(self, payload, callback=None):
if callback is not None:
callback(payload=payload, response=r)

def send(self, recipient_id, message, quick_replies=None, metadata=None, callback=None):
def send(self, recipient_id, message, quick_replies=None, metadata=None,
notification_type=None, callback=None):
text = message if isinstance(message, str) else None
attachment = message if not isinstance(message, str) else None

payload = Payload(recipient=Recipient(id=recipient_id),
message=Message(text=text,
attachment=attachment,
quick_replies=quick_replies,
metadata=metadata))
metadata=metadata),
notification_type=notification_type)

self._send(payload, callback=callback)

def typing_on(self, recipient_id):
payload = Payload(recipient=Recipient(id=recipient_id),
sender_action='typing_on')
sender_action=SenderAction.TYPING_ON)

self._send(payload)

def typing_off(self, recipient_id):
payload = Payload(recipient=Recipient(id=recipient_id),
sender_action='typing_off')
sender_action=SenderAction.TYPING_OFF)

self._send(payload)

def mark_seen(self, recipient_id):
payload = Payload(recipient=Recipient(id=recipient_id),
sender_action='mark_seen')
sender_action=SenderAction.MARK_SEEN)

self._send(payload)

Expand Down
7 changes: 6 additions & 1 deletion fbmq/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ def __init__(self, recipient, message=None, sender_action=None, notification_typ
self.recipient = recipient
self.message = message
if sender_action is not None and sender_action not in ['typing_off', 'typing_on', 'mark_seen']:
raise ValueError('invalud sender_action')
raise ValueError('invalid sender_action : it must be one of "typing_off","typing_on","mark_seen"')

self.sender_action = sender_action

if notification_type is not None \
and notification_type not in ['REGULAR', 'SILENT_PUSH', 'NO_PUSH']:
raise ValueError('invalid notification_type : it must be one of "REGULAR","SILENT_PUSH","NO_PUSH"')

self.notification_type = notification_type

def to_json(self):
Expand Down

0 comments on commit fc08dfe

Please sign in to comment.