From 5c76172b26ec4629041bca45d0a9cfeb5b1f2427 Mon Sep 17 00:00:00 2001 From: Kalebu Date: Sun, 14 Aug 2022 15:36:16 +0300 Subject: [PATCH] updated the webook --- hook.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/hook.py b/hook.py index bcf12e7..6100e3e 100644 --- a/hook.py +++ b/hook.py @@ -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 @@ -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: