wazzup 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
wazzup-0.0.1/LICENCE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2018 The Python Packaging Authority
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
wazzup-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,18 @@
1
+ Metadata-Version: 2.4
2
+ Name: wazzup
3
+ Version: 0.0.1
4
+ Summary: A python package for sending messages using the official Whatsapp API
5
+ Author-email: Rafael model <rafael.model@d7.dev>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/7ws/wazzup
8
+ Project-URL: Issues, https://github.com/7ws/wazzup/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENCE
14
+ Dynamic: license-file
15
+
16
+ # WaZZup
17
+
18
+
wazzup-0.0.1/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # WaZZup
2
+
3
+
@@ -0,0 +1,24 @@
1
+ [tool.isort]
2
+ skip = [".git", "migrations"]
3
+ include_trailing_comma = true
4
+ multi_line_output = 7
5
+
6
+ [project]
7
+ name = "wazzup"
8
+ version = "0.0.1"
9
+ authors = [
10
+ { name="Rafael model", email="rafael.model@d7.dev" },
11
+ ]
12
+ description = "A python package for sending messages using the official Whatsapp API"
13
+ readme = "README.md"
14
+ requires-python = ">=3.9"
15
+ classifiers = [
16
+ "Programming Language :: Python :: 3",
17
+ "Operating System :: OS Independent",
18
+ ]
19
+ license = "MIT"
20
+ license-files = ["LICEN[CS]E*"]
21
+
22
+ [project.urls]
23
+ Homepage = "https://github.com/7ws/wazzup"
24
+ Issues = "https://github.com/7ws/wazzup/issues"
wazzup-0.0.1/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,3 @@
1
+ from .messaging import Messaging
2
+
3
+ __all__ = ["Messaging"]
@@ -0,0 +1,66 @@
1
+ from src.wazzup import models
2
+ from src.wazzup.models.template_message import TemplateMessage
3
+
4
+ from .whatsapp import WhatsAppDriver
5
+
6
+
7
+ class Messaging:
8
+ def __init__(self, access_token, phone_number_id):
9
+ self.whatsapp_driver = WhatsAppDriver(access_token, phone_number_id)
10
+
11
+ def send_text_message(self, text_message: 'models.TextMessage'):
12
+ """
13
+ Send a text message using the WhatsApp API
14
+ """
15
+ response = self.whatsapp_driver.send_text_message(
16
+ text_message=text_message,
17
+ )
18
+
19
+ return response
20
+
21
+ def reply_message(
22
+ self,
23
+ text_message: models.TextMessage,
24
+ message_id,
25
+ ):
26
+ """
27
+ Reply to a message using the WhatsApp API
28
+ """
29
+ response = self.whatsapp_driver.reply_message(text_message, message_id)
30
+
31
+ return response
32
+
33
+ def react_to_message(
34
+ self,
35
+ message_reaction: models.MessageReaction,
36
+ ):
37
+ """
38
+ React to a message using the WhatsApp API
39
+ """
40
+ response = self.whatsapp_driver.react_to_message(message_reaction)
41
+
42
+ return response
43
+
44
+ def send_image_message(self, image_message: models.ImageMessage):
45
+ """
46
+ Send an image message using the WhatsApp API
47
+ """
48
+ response = self.whatsapp_driver.send_image_message(image_message)
49
+
50
+ return response
51
+
52
+ def send_location_message(self, location_message: models.LocationMessage):
53
+ response = self.whatsapp_driver.send_location_message(location_message)
54
+
55
+ return response
56
+
57
+ def send_template_message(self, template: TemplateMessage):
58
+ """
59
+ Send a template message using the WhatsApp API
60
+
61
+ :param recipient_phone_number: The phone number of the recipient.
62
+ :param template_name: The name of the template to be sent.
63
+ """
64
+ response = self.whatsapp_driver.send_template_message(template)
65
+
66
+ return response
@@ -0,0 +1,13 @@
1
+ from .image_message import ImageMessage
2
+ from .location_message import LocationMessage
3
+ from .message_reaction import MessageReaction
4
+ from .template_message import TemplateMessage
5
+ from .text_message import TextMessage
6
+
7
+ __all__ = [
8
+ "ImageMessage",
9
+ "LocationMessage",
10
+ "MessageReaction",
11
+ "TemplateMessage",
12
+ "TextMessage",
13
+ ]
@@ -0,0 +1,23 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class ImageMessage:
6
+
7
+ to: str
8
+ image: dict[str, str]
9
+ messaging_product: str = "whatsapp"
10
+ recipient_type: str = "individual"
11
+ type: str = "image"
12
+
13
+ def __init__(self, id: str, to: str):
14
+ """
15
+ Args:
16
+ message_id: The ID of the message to react to.
17
+ emoji: The emoji to use for the image.
18
+ to: The recipient's phone number in the format 1234567890'.
19
+ """
20
+ self.to = to
21
+ self.image = {
22
+ "id": id,
23
+ }
@@ -0,0 +1,32 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class LocationMessage:
6
+
7
+ to: str
8
+ location: dict[str, str]
9
+ messaging_product: str = "whatsapp"
10
+ type: str = "location"
11
+
12
+ def __init__(
13
+ self,
14
+ to: str,
15
+ longitude: str,
16
+ latitude: str,
17
+ name: str,
18
+ address: str,
19
+ ):
20
+ """
21
+ Args:
22
+ message_id: The ID of the message to react to.
23
+ emoji: The emoji to use for the image.
24
+ to: The recipient's phone number in the format 1234567890'.
25
+ """
26
+ self.to = to
27
+ self.location = {
28
+ 'latitude': latitude,
29
+ 'longitude': longitude,
30
+ 'name': name,
31
+ 'address': address,
32
+ }
@@ -0,0 +1,24 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class MessageReaction:
6
+
7
+ to: str
8
+ reaction: dict[str, str]
9
+ messaging_product: str = "whatsapp"
10
+ recipient_type: str = "individual"
11
+ type: str = "reaction"
12
+
13
+ def __init__(self, message_id: str, emoji, to: str):
14
+ """
15
+ Args:
16
+ message_id: The ID of the message to react to.
17
+ emoji: The emoji to use for the reaction.
18
+ to: The recipient's phone number in the format 1234567890'.
19
+ """
20
+ self.to = to
21
+ self.reaction = {
22
+ "message_id": message_id,
23
+ "emoji": emoji,
24
+ }
@@ -0,0 +1,49 @@
1
+ from dataclasses import asdict, dataclass
2
+
3
+
4
+ @dataclass
5
+ class ComponentHeader:
6
+ parameters: list[dict[str, str]]
7
+ type: str = 'header'
8
+
9
+ def __init__(self, parameters):
10
+ self.parameters = [asdict(parameter) for parameter in parameters]
11
+
12
+
13
+ @dataclass
14
+ class ComponentBody:
15
+ parameters: list[dict[str, str]]
16
+ type: str = 'body'
17
+
18
+ def __init__(self, parameters):
19
+ self.parameters = [asdict(parameter) for parameter in parameters]
20
+
21
+
22
+ @dataclass
23
+ class ComponentFooter:
24
+ parameters: list[dict[str, str]]
25
+ type: str = 'footer'
26
+
27
+ def __init__(self, parameters):
28
+ self.parameters = [asdict(parameter) for parameter in parameters]
29
+
30
+
31
+ @dataclass
32
+ class ComponentButton:
33
+ parameters: list[dict[str, str]]
34
+ sub_type: str = 'quick_reply'
35
+ index: str = '0'
36
+ type: str = 'button'
37
+
38
+ def __init__(self, parameters):
39
+ self.parameters = [asdict(parameter) for parameter in parameters]
40
+
41
+
42
+ @dataclass
43
+ class TemplateComponent:
44
+ type: str
45
+ parameters: list[dict[str, str]]
46
+
47
+ def __init__(self, type: str, parameters):
48
+ self.type = type
49
+ self.parameters = [asdict(parameter) for parameter in parameters]
@@ -0,0 +1,24 @@
1
+ from dataclasses import asdict, dataclass
2
+
3
+ from .template_componenets import TemplateComponent
4
+
5
+
6
+ @dataclass
7
+ class TemplateMessage:
8
+ to: str
9
+ template: dict[str, str]
10
+ messaging_product: str = "whatsapp"
11
+ type: str = "template"
12
+
13
+ def __init__(
14
+ self,
15
+ to: str,
16
+ template_name: str,
17
+ components: list[TemplateComponent],
18
+ ):
19
+ self.to = to
20
+ self.template = {
21
+ 'name': template_name,
22
+ "language": {"code": "pt_BR"},
23
+ 'components': [asdict(component) for component in components]
24
+ }
@@ -0,0 +1,39 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class PayloadParameter:
6
+ payload: str
7
+ type: str = 'payload'
8
+
9
+ def __init__(self, payload: str):
10
+ self.payload = payload
11
+
12
+
13
+ @dataclass
14
+ class ImageParameter:
15
+ image: dict[str, str]
16
+ type: str = 'image'
17
+
18
+ def __init__(self, image_link: str):
19
+ self.image = {'link': image_link}
20
+
21
+
22
+ @dataclass
23
+ class TextParameter:
24
+ text: dict[str, str]
25
+ type: str = 'text'
26
+
27
+ def __init__(self, text_content: str):
28
+ self.text = text_content
29
+
30
+
31
+ @dataclass
32
+ class NamedTextParameter:
33
+ parameter_name: str
34
+ text: str
35
+ type: str = 'text'
36
+
37
+ def __init__(self, parameter_name: str, value: str):
38
+ self.parameter_name = parameter_name
39
+ self.text = value
@@ -0,0 +1,32 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class TextMessage:
6
+ """
7
+ Represents a text message in the WhatsApp application
8
+
9
+ Attributes:
10
+ sender (str): The sender of the message.
11
+ recipient (str): The recipient of the message.
12
+ content (str): The content of the message.
13
+ timestamp (str): The time when the message was sent.
14
+ """
15
+
16
+ to: str
17
+ text: dict[str, str]
18
+ messaging_product: str = "whatsapp"
19
+ recipient_type: str = "individual"
20
+ type: str = "text"
21
+
22
+ def __init__(self, message_content: str, to: str):
23
+ """
24
+ Args:
25
+ message_content: The content of the message.
26
+ to: The recipient of the message.
27
+ """
28
+ self.to = to
29
+ self.text = {
30
+ 'preview_url': False,
31
+ 'body': message_content
32
+ }
@@ -0,0 +1,117 @@
1
+ from dataclasses import asdict
2
+ from urllib.parse import urljoin
3
+
4
+ import requests
5
+
6
+ from src.wazzup import models
7
+
8
+
9
+ class WhatsAppDriver:
10
+ BASE_URL = "https://graph.facebook.com/v22.0/"
11
+ RESOURCE_PATH = "{phone_number}/messages"
12
+
13
+ def __init__(self, access_token, phone_number_id):
14
+ self.access_token = access_token
15
+ self.url = urljoin(
16
+ self.BASE_URL,
17
+ self.RESOURCE_PATH.format(phone_number=phone_number_id),
18
+ )
19
+
20
+ def _get_common_headers(self) -> dict[str, str]:
21
+ """
22
+ Get standard headers for json response
23
+ """
24
+ headers = {
25
+ "Accept": "application/json",
26
+ "Content-Type": "application/json",
27
+ "Authorization": f"Bearer {self.access_token}",
28
+ }
29
+
30
+ return headers
31
+
32
+ def _make_request(
33
+ self,
34
+ method: str,
35
+ url: str,
36
+ data: dict[str, str],
37
+ ) -> None:
38
+ """
39
+ Perform requests to PagBank API endpoints
40
+ """
41
+ headers = self._get_common_headers()
42
+ response = requests.request(method, url, json=data, headers=headers)
43
+ response.raise_for_status()
44
+ return response
45
+
46
+ def send_text_message(self, text_message: models.TextMessage):
47
+ payload = asdict(text_message)
48
+
49
+ response = self._make_request(
50
+ method="POST",
51
+ url=self.url,
52
+ data=payload,
53
+ )
54
+
55
+ return response
56
+
57
+ def reply_message(
58
+ self,
59
+ text_message: models.TextMessage,
60
+ message_id,
61
+ ):
62
+ payload = asdict(text_message)
63
+ payload["context"] = {"message_id": message_id}
64
+
65
+ response = self._make_request(
66
+ method="POST",
67
+ url=self.url,
68
+ data=payload,
69
+ )
70
+
71
+ return response
72
+
73
+ def react_to_message(
74
+ self,
75
+ message_reactoin: models.MessageReaction,
76
+ ):
77
+ payload = asdict(message_reactoin)
78
+ response = self._make_request(
79
+ method="POST",
80
+ url=self.url,
81
+ data=payload,
82
+ )
83
+
84
+ return response
85
+
86
+ def send_image_message(self, image_message: models.ImageMessage):
87
+ payload = asdict(image_message)
88
+
89
+ response = self._make_request(
90
+ method="POST",
91
+ url=self.url,
92
+ data=payload,
93
+ )
94
+
95
+ return response
96
+
97
+ def send_location_message(self, location_message: models.LocationMessage):
98
+ payload = asdict(location_message)
99
+
100
+ response = self._make_request(
101
+ method="POST",
102
+ url=self.url,
103
+ data=payload,
104
+ )
105
+
106
+ return response
107
+
108
+ def send_template_message(self, template: models.TemplateMessage):
109
+ payload = asdict(template)
110
+
111
+ response = self._make_request(
112
+ method="POST",
113
+ url=self.url,
114
+ data=payload,
115
+ )
116
+
117
+ return response
@@ -0,0 +1,18 @@
1
+ Metadata-Version: 2.4
2
+ Name: wazzup
3
+ Version: 0.0.1
4
+ Summary: A python package for sending messages using the official Whatsapp API
5
+ Author-email: Rafael model <rafael.model@d7.dev>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/7ws/wazzup
8
+ Project-URL: Issues, https://github.com/7ws/wazzup/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENCE
14
+ Dynamic: license-file
15
+
16
+ # WaZZup
17
+
18
+
@@ -0,0 +1,22 @@
1
+ LICENCE
2
+ README.md
3
+ pyproject.toml
4
+ src/wazzup/__init__.py
5
+ src/wazzup/messaging.py
6
+ src/wazzup/whatsapp.py
7
+ src/wazzup.egg-info/PKG-INFO
8
+ src/wazzup.egg-info/SOURCES.txt
9
+ src/wazzup.egg-info/dependency_links.txt
10
+ src/wazzup.egg-info/top_level.txt
11
+ src/wazzup/models/__init__.py
12
+ src/wazzup/models/image_message.py
13
+ src/wazzup/models/location_message.py
14
+ src/wazzup/models/message_reaction.py
15
+ src/wazzup/models/template_componenets.py
16
+ src/wazzup/models/template_message.py
17
+ src/wazzup/models/template_parameters.py
18
+ src/wazzup/models/text_message.py
19
+ tests/test_internal.py
20
+ tests/test_send_message.py
21
+ tests/test_send_template_message.py
22
+ tests/test_template_models.py
@@ -0,0 +1,2 @@
1
+ misc
2
+ wazzup
@@ -0,0 +1,39 @@
1
+ from src.wazzup import models
2
+ from src.wazzup.messaging import Messaging
3
+ from src.wazzup.models import template_componenets, template_parameters
4
+
5
+
6
+ class _Test_send_text_message:
7
+ def test_xuxa(self):
8
+ access_token = 'EAAan7AMfBDUBO7FmdB0p7cGLh28GD1SeZBgE4PCQT3RwNIqpbPYvAFDDp2EUwdBH2LugAb9ZCWW9iRVBTV4HfKetL2Pm4h9bs5sK1eP0mnbpQem3rPsxQPcT2TI7ZBghsZCWu9OQE8egCSqPtqZATJdz9mPlc63f9NqxKquLLnwfIjEwDfLkY8gnO4WyZAFTUIuR6DoSPHN7SEkdaVvyeUrmfrNAXrlfVap3wX'
9
+ phone_number_id = '544908905382751'
10
+ recipient_phone_number = '5545999229802'
11
+
12
+ template_message = models.TemplateMessage(
13
+ to=recipient_phone_number,
14
+ template_name='lembrete_para_rafael',
15
+ components=[
16
+ template_componenets.ComponentHeader(
17
+ parameters=[
18
+ template_parameters.NamedTextParameter(
19
+ parameter_name='user_name',
20
+ value='Rafael'
21
+ )
22
+ ]
23
+ ),
24
+ template_componenets.ComponentBody(
25
+ parameters=[
26
+ template_parameters.NamedTextParameter(
27
+ parameter_name='rider_name',
28
+ value='Joana'
29
+ )
30
+ ]
31
+ )
32
+ ]
33
+ )
34
+
35
+ messaging = Messaging(access_token, phone_number_id)
36
+
37
+ response = messaging.send_template_message(template_message)
38
+
39
+ assert response.status_code == 200, response.data
@@ -0,0 +1,245 @@
1
+ from src.wazzup import models
2
+ from src.wazzup.messaging import Messaging
3
+
4
+
5
+ class Test_send_text_message:
6
+ def test_calls_whatsapp_with_expected_data(self, mocker, requests_mock):
7
+ # Prepare
8
+ # Setup requests mock
9
+ requests_mock.request.return_value.status_code = 200
10
+
11
+ access_token = 'my_access_token'
12
+ phone_number_id = '1111111111111111'
13
+ recipient_phone_number = '222222222222222'
14
+ message_content = 'Hello, this is a test message!'
15
+ messaging = Messaging(access_token, phone_number_id)
16
+
17
+ text_message = models.TextMessage(
18
+ message_content=message_content,
19
+ to=recipient_phone_number,
20
+ )
21
+
22
+ # Call
23
+ response = messaging.send_text_message(text_message=text_message)
24
+
25
+ # Assert Response
26
+ assert response.status_code == 200, response.json()
27
+
28
+ # Assert Request
29
+ assert requests_mock.request.call_args_list == [
30
+ mocker.call(
31
+ 'POST',
32
+ f'https://graph.facebook.com/v22.0/{phone_number_id}/messages',
33
+ json={
34
+ 'messaging_product': 'whatsapp',
35
+ 'recipient_type': 'individual',
36
+ 'to': f'{recipient_phone_number}',
37
+ 'type': 'text',
38
+ 'text': {
39
+ 'preview_url': False,
40
+ 'body': f'{message_content}',
41
+ },
42
+ },
43
+ headers={
44
+ 'Accept': 'application/json',
45
+ 'Content-Type': 'application/json',
46
+ 'Authorization': f'Bearer {access_token}'},
47
+ )
48
+ ]
49
+
50
+
51
+ class Test_reply_message:
52
+ def test_calls_whatsapp_with_expected_data(self, mocker, requests_mock):
53
+ # Prepare
54
+ # Setup requests mock
55
+ requests_mock.request.return_value.status_code = 200
56
+
57
+ access_token = 'my_access_token'
58
+ phone_number_id = 'phone_number_id'
59
+ recipient_phone_number = 'recipent_phone_number'
60
+ message_content = 'Hello, this is a test message!'
61
+ message_id = 'message_id'
62
+ messaging = Messaging(access_token, phone_number_id)
63
+
64
+ text_message = models.TextMessage(
65
+ message_content=message_content,
66
+ to=recipient_phone_number,
67
+ )
68
+
69
+ # Call
70
+ response = messaging.reply_message(text_message, message_id)
71
+
72
+ # Assert Response
73
+ assert response.status_code == 200, response.json()
74
+
75
+ # Assert Request
76
+ assert requests_mock.request.call_args_list == [
77
+ mocker.call(
78
+ 'POST',
79
+ f'https://graph.facebook.com/v22.0/{phone_number_id}/messages',
80
+ json={
81
+ 'messaging_product': 'whatsapp',
82
+ 'recipient_type': 'individual',
83
+ 'to': f'{recipient_phone_number}',
84
+ 'type': 'text',
85
+ 'text': {
86
+ 'preview_url': False,
87
+ 'body': f'{message_content}',
88
+ },
89
+ 'context': {
90
+ 'message_id': message_id,
91
+ },
92
+ },
93
+ headers={
94
+ 'Accept': 'application/json',
95
+ 'Content-Type': 'application/json',
96
+ 'Authorization': f'Bearer {access_token}'},
97
+ )
98
+ ]
99
+
100
+
101
+ class Test_react_to_message:
102
+ def test_calls_whatsapp_with_expected_data(self, mocker, requests_mock):
103
+ # Prepare
104
+ # Setup requests mock
105
+ requests_mock.request.return_value.status_code = 200
106
+
107
+ access_token = 'my_access_token'
108
+ phone_number_id = '1111111111111111'
109
+ recipient_phone_number = '222222222222222'
110
+ message_id = 'message_id'
111
+ emoji = '👍'
112
+ messaging = Messaging(access_token, phone_number_id)
113
+
114
+ text_message = models.MessageReaction(
115
+ message_id=message_id,
116
+ emoji=emoji,
117
+ to=recipient_phone_number,
118
+ )
119
+
120
+ # Call
121
+ response = messaging.react_to_message(text_message)
122
+
123
+ # Assert Response
124
+ assert response.status_code == 200, response.json()
125
+
126
+ # Assert Request
127
+ assert requests_mock.request.call_args_list == [
128
+ mocker.call(
129
+ 'POST',
130
+ f'https://graph.facebook.com/v22.0/{phone_number_id}/messages',
131
+ json={
132
+ 'messaging_product': 'whatsapp',
133
+ 'recipient_type': 'individual',
134
+ 'to': f'{recipient_phone_number}',
135
+ 'type': 'reaction',
136
+ 'reaction': {
137
+ 'message_id': message_id,
138
+ 'emoji': emoji,
139
+ },
140
+ },
141
+ headers={
142
+ 'Accept': 'application/json',
143
+ 'Content-Type': 'application/json',
144
+ 'Authorization': f'Bearer {access_token}'},
145
+ )
146
+ ]
147
+
148
+
149
+ class Test_send_image_message:
150
+ def test_calls_whatsapp_with_expected_data(self, mocker, requests_mock):
151
+ # Prepare
152
+ # Setup requests mock
153
+ requests_mock.request.return_value.status_code = 200
154
+
155
+ access_token = 'my_access_token'
156
+ phone_number_id = '1111111111111111'
157
+ recipient_phone_number = '222222222222222'
158
+ image_id = 'image_id'
159
+ messaging = Messaging(access_token, phone_number_id)
160
+
161
+ image_message = models.ImageMessage(
162
+ id=image_id,
163
+ to=recipient_phone_number,
164
+ )
165
+
166
+ # Call
167
+ response = messaging.send_image_message(image_message=image_message)
168
+
169
+ # Assert Response
170
+ assert response.status_code == 200, response.json()
171
+
172
+ # Assert Request
173
+ assert requests_mock.request.call_args_list == [
174
+ mocker.call(
175
+ 'POST',
176
+ f'https://graph.facebook.com/v22.0/{phone_number_id}/messages',
177
+ json={
178
+ 'messaging_product': 'whatsapp',
179
+ 'recipient_type': 'individual',
180
+ 'to': f'{recipient_phone_number}',
181
+ 'type': 'image',
182
+ 'image': {
183
+ 'id': f'{image_id}',
184
+ },
185
+ },
186
+ headers={
187
+ 'Accept': 'application/json',
188
+ 'Content-Type': 'application/json',
189
+ 'Authorization': f'Bearer {access_token}'},
190
+ )
191
+ ]
192
+
193
+
194
+ class Test_send_location_message:
195
+ def test_calls_whatsapp_with_expected_data(self, mocker, requests_mock):
196
+ # Prepare
197
+ # Setup requests mock
198
+ requests_mock.request.return_value.status_code = 200
199
+
200
+ access_token = 'my_access_token'
201
+ phone_number_id = '1111111111111111'
202
+ recipient_phone_number = '222222222222222'
203
+ latitude = '123'
204
+ longitude = '456'
205
+ name = 'Casa da Xuxa'
206
+ address = 'Rua no fim do arcoiris'
207
+ messaging = Messaging(access_token, phone_number_id)
208
+
209
+ location_message = models.LocationMessage(
210
+ to=recipient_phone_number,
211
+ latitude=latitude,
212
+ longitude=longitude,
213
+ name=name,
214
+ address=address,
215
+ )
216
+
217
+ # Call
218
+ response = messaging.send_location_message(location_message)
219
+
220
+ # Assert Response
221
+ assert response.status_code == 200, response.json()
222
+
223
+ # Assert Request
224
+ assert requests_mock.request.call_args_list == [
225
+ mocker.call(
226
+ 'POST',
227
+ f'https://graph.facebook.com/v22.0/{phone_number_id}/messages',
228
+ json={
229
+ 'messaging_product': 'whatsapp',
230
+ 'to': f'{recipient_phone_number}',
231
+ 'type': 'location',
232
+ 'location': {
233
+ 'latitude': latitude,
234
+ 'longitude': longitude,
235
+ 'name': name,
236
+ 'address': address,
237
+ },
238
+ },
239
+ headers={
240
+ 'Accept': 'application/json',
241
+ 'Content-Type': 'application/json',
242
+ 'Authorization': f'Bearer {access_token}'},
243
+ )
244
+ ]
245
+
@@ -0,0 +1,273 @@
1
+ from src.wazzup import models
2
+ from src.wazzup.messaging import Messaging
3
+ from src.wazzup.models import template_componenets, template_parameters
4
+
5
+
6
+ class Test_send_template_with_header_message:
7
+
8
+ def test_calls_whatsapp_with_expected_data(self, mocker, requests_mock):
9
+ # Prepare
10
+ # Setup requests mock
11
+ requests_mock.request.return_value.status_code = 200
12
+
13
+ access_token = 'my_access_token'
14
+ phone_number_id = '1111111111111111'
15
+ recipient_phone_number = '222222222222222'
16
+ template_mame = 'my_template_name'
17
+ messaging = Messaging(access_token, phone_number_id)
18
+
19
+ template_message = models.TemplateMessage(
20
+ to=recipient_phone_number,
21
+ template_name=template_mame,
22
+ components=[
23
+ template_componenets.ComponentHeader(
24
+ parameters=[
25
+ template_parameters.NamedTextParameter(
26
+ parameter_name='my_parameter_name',
27
+ value='My Header Text'
28
+ )
29
+ ]
30
+ )
31
+ ]
32
+ )
33
+
34
+ # Call
35
+ response = messaging.send_template_message(template_message)
36
+
37
+ # Assert Response
38
+ assert response.status_code == 200, response.json()
39
+
40
+ # Assert Request
41
+ assert requests_mock.request.call_args_list == [
42
+ mocker.call(
43
+ 'POST',
44
+ f'https://graph.facebook.com/v22.0/{phone_number_id}/messages',
45
+ json={
46
+ 'messaging_product': 'whatsapp',
47
+ 'to': f'{recipient_phone_number}',
48
+ 'type': 'template',
49
+ 'template': {
50
+ 'name': f'{template_mame}',
51
+ 'language': {'code': 'pt_BR'},
52
+ 'components': [
53
+ {
54
+ 'type': 'header',
55
+ 'parameters': [
56
+ {
57
+ 'type': 'text',
58
+ 'parameter_name': 'my_parameter_name',
59
+ 'text': 'My Header Text'
60
+ }
61
+ ]
62
+ }
63
+ ]
64
+ },
65
+ },
66
+ headers={
67
+ 'Accept': 'application/json',
68
+ 'Content-Type': 'application/json',
69
+ 'Authorization': f'Bearer {access_token}'},
70
+ )
71
+ ]
72
+
73
+
74
+ class Test_send_template_with_body_message:
75
+
76
+ def test_calls_whatsapp_with_expected_data(self, mocker, requests_mock):
77
+ # Prepare
78
+ # Setup requests mock
79
+ requests_mock.request.return_value.status_code = 200
80
+
81
+ access_token = 'my_access_token'
82
+ phone_number_id = '1111111111111111'
83
+ recipient_phone_number = '222222222222222'
84
+ template_mame = 'my_template_name'
85
+ messaging = Messaging(access_token, phone_number_id)
86
+
87
+ template_message = models.TemplateMessage(
88
+ to=recipient_phone_number,
89
+ template_name=template_mame,
90
+ components=[
91
+ template_componenets.ComponentBody(
92
+ parameters=[
93
+ template_parameters.NamedTextParameter(
94
+ parameter_name='my_parameter_name',
95
+ value='My Header Text'
96
+ )
97
+ ]
98
+ )
99
+ ]
100
+ )
101
+
102
+ # Call
103
+ response = messaging.send_template_message(template_message)
104
+
105
+ # Assert Response
106
+ assert response.status_code == 200, response.json()
107
+
108
+ # Assert Request
109
+ assert requests_mock.request.call_args_list == [
110
+ mocker.call(
111
+ 'POST',
112
+ f'https://graph.facebook.com/v22.0/{phone_number_id}/messages',
113
+ json={
114
+ 'messaging_product': 'whatsapp',
115
+ 'to': f'{recipient_phone_number}',
116
+ 'type': 'template',
117
+ 'template': {
118
+ 'name': f'{template_mame}',
119
+ 'language': {'code': 'pt_BR'},
120
+ 'components': [
121
+ {
122
+ 'type': 'body',
123
+ 'parameters': [
124
+ {
125
+ 'type': 'text',
126
+ 'parameter_name': 'my_parameter_name',
127
+ 'text': 'My Header Text'
128
+ }
129
+ ]
130
+ }
131
+ ]
132
+ },
133
+ },
134
+ headers={
135
+ 'Accept': 'application/json',
136
+ 'Content-Type': 'application/json',
137
+ 'Authorization': f'Bearer {access_token}'},
138
+ )
139
+ ]
140
+
141
+
142
+ class Test_send_template_with_footer_message:
143
+
144
+ def test_calls_whatsapp_with_expected_data(self, mocker, requests_mock):
145
+ # Prepare
146
+ # Setup requests mock
147
+ requests_mock.request.return_value.status_code = 200
148
+
149
+ access_token = 'my_access_token'
150
+ phone_number_id = '1111111111111111'
151
+ recipient_phone_number = '222222222222222'
152
+ template_mame = 'my_template_name'
153
+ messaging = Messaging(access_token, phone_number_id)
154
+
155
+ template_message = models.TemplateMessage(
156
+ to=recipient_phone_number,
157
+ template_name=template_mame,
158
+ components=[
159
+ template_componenets.ComponentFooter(
160
+ parameters=[
161
+ template_parameters.NamedTextParameter(
162
+ parameter_name='my_parameter_name',
163
+ value='My Header Text'
164
+ )
165
+ ]
166
+ )
167
+ ]
168
+ )
169
+
170
+ # Call
171
+ response = messaging.send_template_message(template_message)
172
+
173
+ # Assert Response
174
+ assert response.status_code == 200, response.json()
175
+
176
+ # Assert Request
177
+ assert requests_mock.request.call_args_list == [
178
+ mocker.call(
179
+ 'POST',
180
+ f'https://graph.facebook.com/v22.0/{phone_number_id}/messages',
181
+ json={
182
+ 'messaging_product': 'whatsapp',
183
+ 'to': f'{recipient_phone_number}',
184
+ 'type': 'template',
185
+ 'template': {
186
+ 'name': f'{template_mame}',
187
+ 'language': {'code': 'pt_BR'},
188
+ 'components': [
189
+ {
190
+ 'type': 'footer',
191
+ 'parameters': [
192
+ {
193
+ 'type': 'text',
194
+ 'parameter_name': 'my_parameter_name',
195
+ 'text': 'My Header Text'
196
+ }
197
+ ]
198
+ }
199
+ ]
200
+ },
201
+ },
202
+ headers={
203
+ 'Accept': 'application/json',
204
+ 'Content-Type': 'application/json',
205
+ 'Authorization': f'Bearer {access_token}'},
206
+ )
207
+ ]
208
+
209
+
210
+ class Test_send_template_with_button_message:
211
+
212
+ def test_calls_whatsapp_with_expected_data(self, mocker, requests_mock):
213
+ # Prepare
214
+ # Setup requests mock
215
+ requests_mock.request.return_value.status_code = 200
216
+
217
+ access_token = 'my_access_token'
218
+ phone_number_id = '1111111111111111'
219
+ recipient_phone_number = '222222222222222'
220
+ template_mame = 'my_template_name'
221
+ messaging = Messaging(access_token, phone_number_id)
222
+
223
+ template_message = models.TemplateMessage(
224
+ to=recipient_phone_number,
225
+ template_name=template_mame,
226
+ components=[
227
+ template_componenets.ComponentButton(
228
+ parameters=[
229
+ template_parameters.PayloadParameter('My Payload Text')
230
+ ]
231
+ )
232
+ ]
233
+ )
234
+
235
+ # Call
236
+ response = messaging.send_template_message(template_message)
237
+
238
+ # Assert Response
239
+ assert response.status_code == 200, response.json()
240
+
241
+ # Assert Request
242
+ assert requests_mock.request.call_args_list == [
243
+ mocker.call(
244
+ 'POST',
245
+ f'https://graph.facebook.com/v22.0/{phone_number_id}/messages',
246
+ json={
247
+ 'messaging_product': 'whatsapp',
248
+ 'to': f'{recipient_phone_number}',
249
+ 'type': 'template',
250
+ 'template': {
251
+ 'name': f'{template_mame}',
252
+ 'language': {'code': 'pt_BR'},
253
+ 'components': [
254
+ {
255
+ "type": "button",
256
+ "sub_type": "quick_reply",
257
+ "index": "0",
258
+ "parameters": [
259
+ {
260
+ "type": "payload",
261
+ "payload": "My Payload Text",
262
+ },
263
+ ],
264
+ }
265
+ ],
266
+ },
267
+ },
268
+ headers={
269
+ 'Accept': 'application/json',
270
+ 'Content-Type': 'application/json',
271
+ 'Authorization': f'Bearer {access_token}'},
272
+ )
273
+ ]
@@ -0,0 +1,132 @@
1
+ from dataclasses import asdict
2
+
3
+ from src.wazzup.models import template_componenets, template_parameters
4
+
5
+
6
+ class Test_header_template_models:
7
+ def test_with_image_component(self):
8
+ # Prepare
9
+ header = template_componenets.ComponentHeader(
10
+ parameters=[
11
+ template_parameters.ImageParameter('image_link')
12
+ ]
13
+ )
14
+
15
+ # Call
16
+ dict_header = asdict(header)
17
+
18
+ # Assert
19
+ assert dict_header == {
20
+ "type": "header",
21
+ "parameters": [
22
+ {
23
+ "type": "image",
24
+ "image": {
25
+ "link": "image_link"
26
+ },
27
+ },
28
+ ],
29
+ }
30
+
31
+ def test_with_text_component(self):
32
+ # Prepare
33
+ header = template_componenets.ComponentHeader(
34
+ parameters=[
35
+ template_parameters.TextParameter('my text')
36
+ ]
37
+ )
38
+
39
+ # Call
40
+ dict_header = asdict(header)
41
+
42
+ # Assert
43
+ assert dict_header == {
44
+ "type": "header",
45
+ "parameters": [
46
+ {
47
+ "type": "text",
48
+ "text": 'my text'
49
+ },
50
+ ],
51
+ }
52
+
53
+
54
+ class Test_body_template_models:
55
+ def test_with_text_and_image_component(self):
56
+ # Prepare
57
+ body = template_componenets.ComponentBody(
58
+ parameters=[
59
+ template_parameters.TextParameter('my text'),
60
+ template_parameters.ImageParameter('image_link'),
61
+ ]
62
+ )
63
+
64
+ # Call
65
+ dict_body = asdict(body)
66
+
67
+ # Assert
68
+ assert dict_body == {
69
+ "type": "body",
70
+ "parameters": [
71
+ {
72
+ "type": "text",
73
+ "text": 'my text'
74
+ },
75
+ {
76
+ "type": "image",
77
+ "image": {
78
+ "link": "image_link"
79
+ },
80
+ },
81
+ ],
82
+ }
83
+
84
+
85
+ class Test_footer_template_models:
86
+ def test_with_text_component(self):
87
+ # Prepare
88
+ footer = template_componenets.ComponentFooter(
89
+ parameters=[
90
+ template_parameters.TextParameter('my text'),
91
+ ]
92
+ )
93
+
94
+ # Call
95
+ dict_footer = asdict(footer)
96
+
97
+ # Assert
98
+ assert dict_footer == {
99
+ "type": "footer",
100
+ "parameters": [
101
+ {
102
+ "type": "text",
103
+ "text": 'my text'
104
+ },
105
+ ],
106
+ }
107
+
108
+
109
+ class Test_button_template_models:
110
+ def test_with_payload_component(self):
111
+ # Prepare
112
+ payload = template_componenets.ComponentButton(
113
+ parameters=[
114
+ template_parameters.PayloadParameter('my payload'),
115
+ ]
116
+ )
117
+
118
+ # Call
119
+ dict_payload = asdict(payload)
120
+
121
+ # Assert
122
+ assert dict_payload == {
123
+ "type": "button",
124
+ "sub_type": "quick_reply",
125
+ "index": "0",
126
+ "parameters": [
127
+ {
128
+ "type": "payload",
129
+ "payload": "my payload",
130
+ },
131
+ ],
132
+ }