Skip to content

Commit

Permalink
Switch from message to recipient_id as method input (#59)
Browse files Browse the repository at this point in the history
* use raw recipient_id instead of message entry

* fixed pep-style

* tests simplified

* update tests

* remove try-catch in get_user_id, update tests
  • Loading branch information
wittfabian authored and rickydunlop committed Apr 24, 2019
1 parent a524c2b commit a7b4b20
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 173 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release History

## 6.0.0
- Switch from message to recipient_id as method input

## 5.6.1
- Update README
- remove pytest-catchlog from requirements.txt
Expand Down
23 changes: 12 additions & 11 deletions fbmessenger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import six
import requests

__version__ = '5.6.1'
__version__ = '6.0.0'

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -58,7 +58,7 @@ def auth_args(self):
self._auth_args = auth
return self._auth_args

def get_user_data(self, entry, fields=None, timeout=None):
def get_user_data(self, recipient_id, fields=None, timeout=None):
params = {}

if isinstance(fields, six.string_types):
Expand All @@ -71,13 +71,14 @@ def get_user_data(self, entry, fields=None, timeout=None):
params.update(self.auth_args)

r = self.session.get(
'{graph_url}/{sender}'.format(graph_url=self.graph_url, sender=entry['sender']['id']),
'{graph_url}/{recipient_id}'.format(graph_url=self.graph_url, recipient_id=recipient_id),
params=params,
timeout=timeout
)
return r.json()

def send(self, payload, entry, messaging_type='RESPONSE', notification_type='REGULAR', timeout=None, tag=None):
def send(self, payload, recipient_id, messaging_type='RESPONSE', notification_type='REGULAR',
timeout=None, tag=None):
if messaging_type not in self.MESSAGING_TYPES:
raise ValueError('`{}` is not a valid `messaging_type`'.format(messaging_type))

Expand All @@ -88,9 +89,9 @@ def send(self, payload, entry, messaging_type='RESPONSE', notification_type='REG
'messaging_type': messaging_type,
'notification_type': notification_type,
'recipient': {
'id': entry['sender']['id']
'id': recipient_id,
},
'message': payload
'message': payload,
}

if tag:
Expand All @@ -104,13 +105,13 @@ def send(self, payload, entry, messaging_type='RESPONSE', notification_type='REG
)
return r.json()

def send_action(self, sender_action, entry, timeout=None):
def send_action(self, sender_action, recipient_id, timeout=None):
r = self.session.post(
'{graph_url}/me/messages'.format(graph_url=self.graph_url),
params=self.auth_args,
json={
'recipient': {
'id': entry['sender']['id']
'id': recipient_id,
},
'sender_action': sender_action
},
Expand Down Expand Up @@ -288,14 +289,14 @@ def handle(self, payload):
return self.read(message)

def get_user(self, fields=None, timeout=None):
return self.client.get_user_data(self.last_message, fields=fields, timeout=timeout)
return self.client.get_user_data(self.get_user_id(), fields=fields, timeout=timeout)

def send(self, payload, messaging_type='RESPONSE', notification_type='REGULAR', timeout=None, tag=None):
return self.client.send(payload, self.last_message, messaging_type=messaging_type,
return self.client.send(payload, self.get_user_id(), messaging_type=messaging_type,
notification_type=notification_type, timeout=timeout, tag=tag)

def send_action(self, sender_action, timeout=None):
return self.client.send_action(sender_action, self.last_message, timeout=timeout)
return self.client.send_action(sender_action, self.get_user_id(), timeout=timeout)

def get_user_id(self):
return self.last_message['sender']['id']
Expand Down
Loading

0 comments on commit a7b4b20

Please sign in to comment.