Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +92 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
from twilio.rest import Client
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# --------------------------------------------------------------------
|
| 9 |
+
# Step 1: Load API Keys from Hugging Face Secrets
|
| 10 |
+
# --------------------------------------------------------------------
|
| 11 |
+
TW_SID = os.environ.get("TWILIO_SID")
|
| 12 |
+
TW_TOKEN = os.environ.get("TWILIO_TOKEN")
|
| 13 |
+
GNEWS_KEY = os.environ.get("GNEWS_API_KEY")
|
| 14 |
+
|
| 15 |
+
if not (TW_SID and TW_TOKEN and GNEWS_KEY):
|
| 16 |
+
raise ValueError("Please set TWILIO_SID, TWILIO_TOKEN, and GNEWS_API_KEY as Secrets in Hugging Face.")
|
| 17 |
+
|
| 18 |
+
# --------------------------------------------------------------------
|
| 19 |
+
# Step 2: Twilio WhatsApp sender number
|
| 20 |
+
# --------------------------------------------------------------------
|
| 21 |
+
TWILIO_WHATSAPP_FROM = "whatsapp:+14155238886"
|
| 22 |
+
MAX_CHARS = 1500 # safe limit per WhatsApp message
|
| 23 |
+
|
| 24 |
+
# --------------------------------------------------------------------
|
| 25 |
+
# Step 3: Function to fetch and send news
|
| 26 |
+
# --------------------------------------------------------------------
|
| 27 |
+
def send_news(whatsapp_number: str, city: str):
|
| 28 |
+
city = city.strip()
|
| 29 |
+
if not city:
|
| 30 |
+
return "Please enter a valid city."
|
| 31 |
+
|
| 32 |
+
url = f"https://gnews.io/api/v4/search?q={city}&lang=en&country=in&max=15&apikey={GNEWS_KEY}"
|
| 33 |
+
r = requests.get(url)
|
| 34 |
+
if not r.ok:
|
| 35 |
+
return f"Failed to fetch news ({r.status_code}): {r.text}"
|
| 36 |
+
|
| 37 |
+
news_data = r.json().get("articles", [])
|
| 38 |
+
if not news_data:
|
| 39 |
+
return f"No news found for {city}."
|
| 40 |
+
|
| 41 |
+
# Prepare messages under character limit
|
| 42 |
+
messages = []
|
| 43 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M")
|
| 44 |
+
current_msg = f"📰 *Top News in {city}* (Fetched: {timestamp})
|
| 45 |
+
|
| 46 |
+
"
|
| 47 |
+
|
| 48 |
+
for i, article in enumerate(news_data, start=1):
|
| 49 |
+
title = article.get("title", "No title")
|
| 50 |
+
vk_code = f"VK{i}" # custom short code
|
| 51 |
+
line = f"{i}. *{title}* ⚡
|
| 52 |
+
{vk_code}
|
| 53 |
+
|
| 54 |
+
"
|
| 55 |
+
|
| 56 |
+
if len(current_msg) + len(line) > MAX_CHARS:
|
| 57 |
+
messages.append(current_msg.strip())
|
| 58 |
+
current_msg = line
|
| 59 |
+
else:
|
| 60 |
+
current_msg += line
|
| 61 |
+
if current_msg:
|
| 62 |
+
messages.append(current_msg.strip())
|
| 63 |
+
|
| 64 |
+
# Prepare WhatsApp number
|
| 65 |
+
TO = whatsapp_number.strip()
|
| 66 |
+
if not TO.startswith("whatsapp:"):
|
| 67 |
+
TO = f"whatsapp:{TO}"
|
| 68 |
+
|
| 69 |
+
client = Client(TW_SID, TW_TOKEN)
|
| 70 |
+
try:
|
| 71 |
+
sids = []
|
| 72 |
+
for msg_text in messages:
|
| 73 |
+
msg = client.messages.create(body=msg_text, from_=TWILIO_WHATSAPP_FROM, to=TO)
|
| 74 |
+
sids.append(msg.sid)
|
| 75 |
+
return f"Message(s) sent successfully! Message SIDs: {', '.join(sids)}"
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"Failed to send WhatsApp message: {e}"
|
| 78 |
+
|
| 79 |
+
# --------------------------------------------------------------------
|
| 80 |
+
# Step 4: Gradio UI
|
| 81 |
+
# --------------------------------------------------------------------
|
| 82 |
+
with gr.Blocks() as demo:
|
| 83 |
+
gr.Markdown("## TN News Feed Over WhatsApp")
|
| 84 |
+
whatsapp_input = gr.Textbox(label="Your WhatsApp number (e.g. +91XXXXXXXXXX)", placeholder="+91XXXXXXXXXX")
|
| 85 |
+
city_input = gr.Textbox(label="City in Tamil Nadu", placeholder="Coimbatore")
|
| 86 |
+
send_button = gr.Button("Send News")
|
| 87 |
+
output = gr.Textbox(label="Status")
|
| 88 |
+
|
| 89 |
+
send_button.click(fn=send_news, inputs=[whatsapp_input, city_input], outputs=output)
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
gradio>=3.50
|
| 3 |
+
requests
|
| 4 |
+
twilio
|