John Landry commited on
Commit
220afd8
1 Parent(s): 448b36e

return message

Browse files
Files changed (6) hide show
  1. .gitignore +2 -1
  2. __pycache__/main.cpython-310.pyc +0 -0
  3. main.py +14 -5
  4. requirements.txt +2 -1
  5. test.py +46 -0
  6. whatsapp_client.py +60 -0
.gitignore CHANGED
@@ -1,4 +1,5 @@
1
  bin/
2
  lib/
3
- __pycache__/
 
4
  pyvenv.cfg
 
1
  bin/
2
  lib/
3
+ __pycache__/
4
+ .env
5
  pyvenv.cfg
__pycache__/main.cpython-310.pyc CHANGED
Binary files a/__pycache__/main.cpython-310.pyc and b/__pycache__/main.cpython-310.pyc differ
 
main.py CHANGED
@@ -1,6 +1,6 @@
1
- from fastapi import FastAPI, Request, Query, Body
2
- from typing import List
3
  import logging
 
4
 
5
  logger = logging.getLogger()
6
 
@@ -30,8 +30,17 @@ async def verify(
30
 
31
  @app.post("/webook")
32
  async def callback(request: Request):
33
- print("callback is being called")
34
- print(f"HEADERS [{request.headers}]")
35
- print(f"BODY [{await request.body()}]")
 
 
 
 
 
 
 
 
 
36
  return {"status": "success"}
37
 
 
1
+ from fastapi import FastAPI, Request, Query
 
2
  import logging
3
+ from whatsapp_client import WhatsAppWrapper
4
 
5
  logger = logging.getLogger()
6
 
 
30
 
31
  @app.post("/webook")
32
  async def callback(request: Request):
33
+ print("============ callback is being called ============")
34
+ data = await request.body()
35
+
36
+ phone_num = data['entry'][0]['changes'][0]['value']['messages'][0]['from']
37
+ mesg_body = data['entry'][0]['changes'][0]['value']['messages'][0]['text']['body']
38
+ phone_number_id = data['entry'][0]['changes'][0]['value']['metadata']['phone_number_id']
39
+
40
+ print(f"PHONE# {phone_num} \nMSG: {mesg_body}\nPONE_NUM_ID: {phone_number_id}")
41
+
42
+ client = WhatsAppWrapper()
43
+ client.send_template_message("hello_world", "en_US", phone_num)
44
+ client.send_text_message("hello, right back at ya ..." + mesg_body, phone_num)
45
  return {"status": "success"}
46
 
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  fastapi
2
- uvicorn[standard]
 
 
1
  fastapi
2
+ uvicorn[standard]
3
+ python-dotenv
test.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ data = {
2
+ "object": "whatsapp_business_account",
3
+ "entry": [
4
+ {
5
+ "id": "270451259495375",
6
+ "changes": [
7
+ {
8
+ "value": {
9
+ "messaging_product": "whatsapp",
10
+ "metadata": {
11
+ "display_phone_number": "15556024077",
12
+ "phone_number_id": "297527496785471"
13
+ },
14
+ "contacts": [
15
+ {
16
+ "profile": {
17
+ "name": "Eric Landry"
18
+ },
19
+ "wa_id": "15127404620"
20
+ }
21
+ ],
22
+ "messages": [
23
+ {
24
+ "from": "15127404620",
25
+ "id": "wamid.HBgLMTUxMjc0MDQ2MjAVAgASGBQzQTEyQTM0OURGQUI4QzZDNTg2MAA=",
26
+ "timestamp": "1716933996",
27
+ "text": {
28
+ "body": "hello"
29
+ },
30
+ "type": "text"
31
+ }
32
+ ]
33
+ },
34
+ "field": "messages"
35
+ }
36
+ ]
37
+ }
38
+ ]
39
+ }
40
+
41
+ phone_num = data['entry'][0]['changes'][0]['value']['messages'][0]['from']
42
+ mesg_body = data['entry'][0]['changes'][0]['value']['messages'][0]['text']['body']
43
+ phone_number_id = data['entry'][0]['changes'][0]['value']['metadata']['phone_number_id']
44
+
45
+
46
+ print(f"PHONE# {phone_num} \nMSG: {mesg_body}\nPONE_NUM_ID: {phone_number_id}")
whatsapp_client.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # whatsapp_client.py
2
+ import os
3
+ import requests
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+
8
+ class WhatsAppWrapper:
9
+
10
+ API_URL = "https://graph.facebook.com/v19.0/"
11
+ WHATSAPP_API_TOKEN = os.environ.get("WHATSAPP_API_TOKEN")
12
+ WHATSAPP_CLOUD_NUMBER_ID = os.environ.get("WHATSAPP_CLOUD_NUMBER_ID")
13
+
14
+ def __init__(self):
15
+ self.headers = {
16
+ "Authorization": f"Bearer {self.WHATSAPP_API_TOKEN}",
17
+ "Content-Type": "application/json",
18
+ }
19
+ self.API_URL = self.API_URL + self.WHATSAPP_CLOUD_NUMBER_ID
20
+
21
+ def send_template_message(self, template_name, language_code, phone_number):
22
+
23
+ payload = {
24
+ "messaging_product": "whatsapp",
25
+ "to": phone_number,
26
+ "type": "template",
27
+ "template": {
28
+ "name": template_name,
29
+ "language": {
30
+ "code": language_code
31
+ }
32
+ }
33
+ }
34
+
35
+ response = requests.post(f"{self.API_URL}/messages", json=payload,headers=self.headers)
36
+ print(response)
37
+ assert response.status_code == 200, "Error sending message"
38
+
39
+ return response.status_code
40
+
41
+ def send_text_message(self,message, phone_number):
42
+ payload = {
43
+ "messaging_product": 'whatsapp',
44
+ "to": phone_number,
45
+ "type": "text",
46
+ "text": {
47
+ "preview_url": False,
48
+ "body": message
49
+ }
50
+ }
51
+ response = requests.post(f"{self.API_URL}/messages", json=payload,headers=self.headers)
52
+ print(response.status_code)
53
+ print(response.text)
54
+ assert response.status_code == 200, "Error sending message"
55
+ return response.status_code
56
+
57
+ if __name__ == "__main__":
58
+ client = WhatsAppWrapper()
59
+ # send a template message
60
+ client.send_template_message("hello_world", "en_US", "+15127404620")