9,862
edits
No edit summary |
No edit summary |
||
Line 100: | Line 100: | ||
#* Outbound SMS costs can escalate. Consider funding models or sponsorships. | #* Outbound SMS costs can escalate. Consider funding models or sponsorships. | ||
---- | ----from flask import Flask, request, jsonify | ||
import requests | |||
from bs4 import BeautifulSoup | |||
app = Flask(__name__) | |||
<nowiki>#</nowiki> Facebook API credentials | |||
PAGE_ACCESS_TOKEN = 'your_page_access_token' | |||
<nowiki>#</nowiki> Function to send messages via Messenger | |||
def send_message(recipient_id, message_text): | |||
url = f"<nowiki>https://graph.facebook.com/v11.0/me/messages?access_token={PAGE_ACCESS_TOKEN}</nowiki>" | |||
headers = {'Content-Type': 'application/json'} | |||
payload = { | |||
'recipient': {'id': recipient_id}, | |||
'message': {'text': message_text} | |||
} | |||
response = requests.post(url, json=payload, headers=headers) | |||
return response.json() | |||
<nowiki>#</nowiki> Function to fetch and simplify web content | |||
def fetch_web_content(url): | |||
try: | |||
response = requests.get(url, timeout=5) | |||
response.raise_for_status() | |||
soup = BeautifulSoup(response.text, 'html.parser') | |||
# Extract title and paragraph text | |||
title = soup.title.string if soup.title else "No Title" | |||
paragraphs = soup.find_all('p') | |||
text_content = " ".join(p.get_text(strip=True) for p in paragraphs[:3]) # Limit to 3 paragraphs | |||
# Simplify links | |||
links = soup.find_all('a', href=True) | |||
links_text = [f"{a.get_text(strip=True)}: {a['href']}" for a in links[:3]] # Limit to 3 links | |||
# Prepare summary | |||
summary = f"Title: {title}\nContent: {text_content}\nLinks:\n" + "\n".join(links_text) | |||
return summary[:2000] # Limit to 2000 characters for Messenger | |||
except Exception as e: | |||
return f"Error fetching content: {e}" | |||
<nowiki>#</nowiki> Webhook to handle incoming messages | |||
@app.route('/webhook', methods=['POST']) | |||
def webhook(): | |||
data = request.json | |||
if data['object'] == 'page': | |||
for entry in data['entry']: | |||
for messaging_event in entry['messaging']: | |||
sender_id = messaging_event['sender']['id'] | |||
if 'message' in messaging_event and 'text' in messaging_event['message']: | |||
user_message = messaging_event['message']['text'] | |||
# Fetch and simplify web content | |||
if user_message.startswith("URL:"): | |||
url = user_message[4:].strip() | |||
content = fetch_web_content(url) | |||
else: | |||
content = "Send a message in the format 'URL:<website>' to fetch content." | |||
# Respond to user | |||
send_message(sender_id, content) | |||
return "ok", 200 | |||
<nowiki>#</nowiki> Facebook verification endpoint | |||
@app.route('/webhook', methods=['GET']) | |||
def verify_webhook(): | |||
VERIFY_TOKEN = 'your_verify_token' | |||
mode = request.args.get('hub.mode') | |||
token = request.args.get('hub.verify_token') | |||
challenge = request.args.get('hub.challenge') | |||
if mode == 'subscribe' and token == VERIFY_TOKEN: | |||
return challenge, 200 | |||
else: | |||
return 'Forbidden', 403 | |||
if __name__ == '__main__': | |||
app.run(port=5000) |