|
1 | 1 | import json |
| 2 | + |
2 | 3 | import requests |
3 | 4 | from requests_toolbelt import MultipartEncoder |
4 | 5 |
|
5 | | -DEFAULT_API_VERSION = 2.6 |
| 6 | +from pymessenger.graph_api import FacebookGraphApi |
| 7 | +import pymessenger.utils as utils |
| 8 | + |
6 | 9 |
|
7 | | -class Bot(object): |
8 | | - def __init__(self, access_token, api_version=DEFAULT_API_VERSION, app_secret=None): |
9 | | - self.api_version = api_version |
10 | | - self.access_token = access_token |
11 | | - self.base_url = ( |
12 | | - "https://graph.facebook.com" |
13 | | - "/v{0}/me/messages?access_token={1}" |
14 | | - ).format(self.api_version, access_token) |
| 10 | +class Bot(FacebookGraphApi): |
15 | 11 |
|
16 | | - if app_secret is not None: |
17 | | - appsecret_proof = generate_appsecret_proof(access_token, app_secret) |
18 | | - self.base_url += '&appsecret_proof={0}'.format(appsecret_proof) |
| 12 | + def __init__(self, *args, **kwargs): |
| 13 | + super(Bot, self).__init__(*args, **kwargs) |
19 | 14 |
|
20 | 15 | def send_text_message(self, recipient_id, message): |
21 | 16 | payload = { |
@@ -72,10 +67,6 @@ def send_button_message(self, recipient_id, text, buttons): |
72 | 67 | } |
73 | 68 | return self._send_payload(payload) |
74 | 69 |
|
75 | | - def _send_payload(self, payload): |
76 | | - result = requests.post(self.base_url, json=payload).json() |
77 | | - return result |
78 | | - |
79 | 70 | def send_image(self, recipient_id, image_path): |
80 | 71 | ''' |
81 | 72 | This sends an image to the specified recipient. |
@@ -135,3 +126,15 @@ def send_image_url(self, recipient_id, image_url): |
135 | 126 | ) |
136 | 127 | } |
137 | 128 | return self._send_payload(payload) |
| 129 | + |
| 130 | + def _send_payload(self, payload): |
| 131 | + request_endpoint = '{0}/me/messages'.format(self.graph_url) |
| 132 | + response = requests.post( |
| 133 | + request_endpoint, |
| 134 | + params=self.auth_args, |
| 135 | + json=payload |
| 136 | + ) |
| 137 | + result = response.json() |
| 138 | + return result |
| 139 | + |
| 140 | + |
0 commit comments