codeboosterstech commited on
Commit
77de0ff
·
verified ·
1 Parent(s): 828ca8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import os
3
  import requests
4
  from twilio.rest import Client
@@ -6,7 +5,7 @@ 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")
@@ -16,13 +15,13 @@ 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()
@@ -39,22 +38,22 @@ def send_news(whatsapp_number: str, city: str):
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})\n\n"
45
-
46
- for i, article in enumerate(news_data, start=1):
47
- title = article.get("title", "No title")
48
- vk_code = f"VK{i}" # custom short code
49
- line = f"{i}. *{title}* ⚡\n{vk_code}\n\n"
50
 
51
- if len(current_msg) + len(line) > MAX_CHARS:
 
 
 
 
 
 
 
 
 
 
52
  messages.append(current_msg.strip())
53
- current_msg = line
54
- else:
55
- current_msg += line
56
- if current_msg:
57
- messages.append(current_msg.strip())
58
 
59
  # Prepare WhatsApp number
60
  TO = whatsapp_number.strip()
@@ -72,7 +71,7 @@ if current_msg:
72
  return f"Failed to send WhatsApp message: {e}"
73
 
74
  # --------------------------------------------------------------------
75
- # Step 4: Gradio UI
76
  # --------------------------------------------------------------------
77
  with gr.Blocks() as demo:
78
  gr.Markdown("## TN News Feed Over WhatsApp")
 
 
1
  import os
2
  import requests
3
  from twilio.rest import Client
 
5
  from datetime import datetime
6
 
7
  # --------------------------------------------------------------------
8
+ # Load API Keys from Hugging Face Secrets
9
  # --------------------------------------------------------------------
10
  TW_SID = os.environ.get("TWILIO_SID")
11
  TW_TOKEN = os.environ.get("TWILIO_TOKEN")
 
15
  raise ValueError("Please set TWILIO_SID, TWILIO_TOKEN, and GNEWS_API_KEY as Secrets in Hugging Face.")
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()
 
38
  return f"No news found for {city}."
39
 
40
  # Prepare messages under character limit
41
+ messages = []
42
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M")
43
+ current_msg = f"📰 *Top News in {city}* (Fetched: {timestamp})\n\n"
 
 
 
 
 
44
 
45
+ for i, article in enumerate(news_data, start=1):
46
+ title = article.get("title", "No title")
47
+ vk_code = f"VK{i}" # custom short code
48
+ line = f"{i}. *{title}* ⚡\n{vk_code}\n\n"
49
+
50
+ if len(current_msg) + len(line) > MAX_CHARS:
51
+ messages.append(current_msg.strip())
52
+ current_msg = line
53
+ else:
54
+ current_msg += line
55
+ if current_msg:
56
  messages.append(current_msg.strip())
 
 
 
 
 
57
 
58
  # Prepare WhatsApp number
59
  TO = whatsapp_number.strip()
 
71
  return f"Failed to send WhatsApp message: {e}"
72
 
73
  # --------------------------------------------------------------------
74
+ # Gradio UI
75
  # --------------------------------------------------------------------
76
  with gr.Blocks() as demo:
77
  gr.Markdown("## TN News Feed Over WhatsApp")