Skip to content

Commit

Permalink
updated the webook
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalebu committed Aug 14, 2022
1 parent 72f757d commit 5c76172
Showing 1 changed file with 58 additions and 11 deletions.
69 changes: 58 additions & 11 deletions hook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import json
from platform import python_version_tuple
import logging
from heyoo import WhatsApp
from dotenv import load_dotenv
from flask import Flask, request, make_response
Expand All @@ -10,44 +9,92 @@

# Load .env file
load_dotenv()
<<<<<<< HEAD
messenger = WhatsApp(os.getenv("TOKEN"), phone_number_id=os.getenv("PHONE_NUMBER_ID"))
=======

messenger = WhatsApp(os.getenv("TOKEN"),phone_number_id='104xxxxxx')
>>>>>>> dd85c2baf3ce2f83023a69f08f89e242bf29d6ed
VERIFY_TOKEN = "30cca545-3838-48b2-80a7-9e43b1ae8ce4"

# Logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)


@app.route("/", methods=["GET", "POST"])
def hook():
if request.method == "GET":
if request.args.get("hub.verify_token") == VERIFY_TOKEN:
logging.info("Verified webhook")
response = make_response(request.args.get("hub.challenge"), 200)
response.mimetype = "text/plain"
return response
logging.error("Webhook Verification failed")
return "Invalid verification token"

# Handle Webhook Subscriptions
data = request.get_json()
logging.info("Received webhook data: %s", data)
changed_field = messenger.changed_field(data)
if changed_field == "messages":
new_message = messenger.get_mobile(data)
if new_message:
mobile = messenger.get_mobile(data)
name = messenger.get_name(data)
message_type = messenger.get_message_type(data)

logging.info(
f"New Message; sender:{mobile} name:{name} type:{message_type}"
)
if message_type == "text":
message = messenger.get_message(data)
name = messenger.get_name(data)
print(f"{name} with this {mobile} number sent {message}")
logging.info("Message: %s", message)
messenger.send_message(f"Hi {name}, nice to connect with you", mobile)

elif message_type == "interactive":
message_response = messenger.get_interactive_response(data)
print(message_response)
intractive_type = message_response.get("type")
message_id = message_response[intractive_type]["id"]
message_text = message_response[intractive_type]["title"]
logging.info(f"Interactive Message; {message_id}: {message_text}")

elif message_type == "location":
message_location = messenger.get_location(data)
message_latitude = message_location["latitude"]
message_longitude = message_location["longitude"]
logging.info("Location: %s, %s", message_latitude, message_longitude)

elif message_type == "image":
image = messenger.get_image(data)
image_id, mime_type = image["id"], image["mime_type"]
image_url = messenger.query_media_url(image_id)
image_filename = messenger.download_media(image_url, mime_type)
print(f"{mobile} sent image {image_filename}")
logging.info(f"{mobile} sent image {image_filename}")

elif message_type == "video":
video = messenger.get_video(data)
video_id, mime_type = video["id"], video["mime_type"]
video_url = messenger.query_media_url(video_id)
video_filename = messenger.download_media(video_url, mime_type)
print(f"{mobile} sent video {video_filename}")
logging.info(f"{mobile} sent video {video_filename}")

elif message_type == "audio":
audio = messenger.get_audio(data)
audio_id, mime_type = audio["id"], audio["mime_type"]
audio_url = messenger.query_media_url(audio_id)
audio_filename = messenger.download_media(audio_url, mime_type)
print(f"{mobile} sent audio {audio_filename}")
logging.info(f"{mobile} sent audio {audio_filename}")

elif message_type == "file":
file = messenger.get_file(data)
file_id, mime_type = file["id"], file["mime_type"]
file_url = messenger.query_media_url(file_id)
file_filename = messenger.download_media(file_url, mime_type)
print(f"{mobile} sent file {file_filename}")
logging.info(f"{mobile} sent file {file_filename}")
else:
pass
print(f"{mobile} sent {message_type} ")
print(data)
else:
delivery = messenger.get_delivery(data)
if delivery:
Expand Down

0 comments on commit 5c76172

Please sign in to comment.