Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,47 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
|
|
|
| 3 |
from twilio.rest import Client
|
| 4 |
import gradio as gr
|
| 5 |
from datetime import datetime
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# --------------------------------------------------------------------
|
| 8 |
-
#
|
| 9 |
# --------------------------------------------------------------------
|
| 10 |
TW_SID = os.environ.get("TWILIO_SID")
|
| 11 |
TW_TOKEN = os.environ.get("TWILIO_TOKEN")
|
| 12 |
GNEWS_KEY = os.environ.get("GNEWS_API_KEY")
|
| 13 |
|
| 14 |
-
if not (TW_SID and TW_TOKEN
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# --------------------------------------------------------------------
|
| 18 |
-
# Twilio WhatsApp sender number
|
| 19 |
# --------------------------------------------------------------------
|
| 20 |
TWILIO_WHATSAPP_FROM = "whatsapp:+14155238886"
|
| 21 |
MAX_CHARS = 1500 # safe limit per WhatsApp message
|
| 22 |
|
| 23 |
# --------------------------------------------------------------------
|
| 24 |
-
# Function to fetch and send news
|
| 25 |
# --------------------------------------------------------------------
|
| 26 |
def send_news(whatsapp_number: str, city: str):
|
| 27 |
city = city.strip()
|
|
@@ -44,8 +64,9 @@ def send_news(whatsapp_number: str, city: str):
|
|
| 44 |
|
| 45 |
for i, article in enumerate(news_data, start=1):
|
| 46 |
title = article.get("title", "No title")
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
if len(current_msg) + len(line) > MAX_CHARS:
|
| 51 |
messages.append(current_msg.strip())
|
|
@@ -71,10 +92,10 @@ def send_news(whatsapp_number: str, city: str):
|
|
| 71 |
return f"Failed to send WhatsApp message: {e}"
|
| 72 |
|
| 73 |
# --------------------------------------------------------------------
|
| 74 |
-
# Gradio UI
|
| 75 |
# --------------------------------------------------------------------
|
| 76 |
with gr.Blocks() as demo:
|
| 77 |
-
gr.Markdown("##
|
| 78 |
whatsapp_input = gr.Textbox(label="Your WhatsApp number (e.g. +91XXXXXXXXXX)", placeholder="+91XXXXXXXXXX")
|
| 79 |
city_input = gr.Textbox(label="City in Tamil Nadu", placeholder="Coimbatore")
|
| 80 |
send_button = gr.Button("Send News")
|
|
@@ -82,5 +103,4 @@ with gr.Blocks() as demo:
|
|
| 82 |
|
| 83 |
send_button.click(fn=send_news, inputs=[whatsapp_input, city_input], outputs=output)
|
| 84 |
|
| 85 |
-
|
| 86 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
+
from getpass import getpass
|
| 4 |
from twilio.rest import Client
|
| 5 |
import gradio as gr
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
+
# TinyURL function
|
| 9 |
+
def shorten_url(url):
|
| 10 |
+
try:
|
| 11 |
+
r = requests.get(f"http://tinyurl.com/api-create.php?url={url}")
|
| 12 |
+
if r.ok:
|
| 13 |
+
return r.text
|
| 14 |
+
return url
|
| 15 |
+
except:
|
| 16 |
+
return url
|
| 17 |
+
|
| 18 |
# --------------------------------------------------------------------
|
| 19 |
+
# Step 1: Load / Input API Keys
|
| 20 |
# --------------------------------------------------------------------
|
| 21 |
TW_SID = os.environ.get("TWILIO_SID")
|
| 22 |
TW_TOKEN = os.environ.get("TWILIO_TOKEN")
|
| 23 |
GNEWS_KEY = os.environ.get("GNEWS_API_KEY")
|
| 24 |
|
| 25 |
+
if not (TW_SID and TW_TOKEN):
|
| 26 |
+
print("Enter your Twilio credentials:")
|
| 27 |
+
TW_SID = getpass("Twilio SID: ").strip()
|
| 28 |
+
TW_TOKEN = getpass("Twilio Auth Token: ").strip()
|
| 29 |
+
os.environ["TWILIO_SID"] = TW_SID
|
| 30 |
+
os.environ["TWILIO_TOKEN"] = TW_TOKEN
|
| 31 |
+
|
| 32 |
+
if not GNEWS_KEY:
|
| 33 |
+
print("Enter your GNews API key (get it from https://gnews.io/):")
|
| 34 |
+
GNEWS_KEY = getpass("GNews API Key: ").strip()
|
| 35 |
+
os.environ["GNEWS_API_KEY"] = GNEWS_KEY
|
| 36 |
|
| 37 |
# --------------------------------------------------------------------
|
| 38 |
+
# Step 2: Twilio WhatsApp sender number
|
| 39 |
# --------------------------------------------------------------------
|
| 40 |
TWILIO_WHATSAPP_FROM = "whatsapp:+14155238886"
|
| 41 |
MAX_CHARS = 1500 # safe limit per WhatsApp message
|
| 42 |
|
| 43 |
# --------------------------------------------------------------------
|
| 44 |
+
# Step 3: Function to fetch and send news (enhanced formatting)
|
| 45 |
# --------------------------------------------------------------------
|
| 46 |
def send_news(whatsapp_number: str, city: str):
|
| 47 |
city = city.strip()
|
|
|
|
| 64 |
|
| 65 |
for i, article in enumerate(news_data, start=1):
|
| 66 |
title = article.get("title", "No title")
|
| 67 |
+
url = article.get("url", "")
|
| 68 |
+
short_url = shorten_url(url)
|
| 69 |
+
line = f"{i}. *{title}* ⚡\n{short_url}\n\n"
|
| 70 |
|
| 71 |
if len(current_msg) + len(line) > MAX_CHARS:
|
| 72 |
messages.append(current_msg.strip())
|
|
|
|
| 92 |
return f"Failed to send WhatsApp message: {e}"
|
| 93 |
|
| 94 |
# --------------------------------------------------------------------
|
| 95 |
+
# Step 4: Gradio UI
|
| 96 |
# --------------------------------------------------------------------
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
+
gr.Markdown("## Send latest 15 news of any Tamil Nadu city to WhatsApp")
|
| 99 |
whatsapp_input = gr.Textbox(label="Your WhatsApp number (e.g. +91XXXXXXXXXX)", placeholder="+91XXXXXXXXXX")
|
| 100 |
city_input = gr.Textbox(label="City in Tamil Nadu", placeholder="Coimbatore")
|
| 101 |
send_button = gr.Button("Send News")
|
|
|
|
| 103 |
|
| 104 |
send_button.click(fn=send_news, inputs=[whatsapp_input, city_input], outputs=output)
|
| 105 |
|
| 106 |
+
demo.launch()
|
|
|