measmonysuon commited on
Commit
29e9450
·
verified ·
1 Parent(s): cd1df65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -31,6 +31,18 @@ logging.basicConfig(
31
  )
32
  logger = logging.getLogger(__name__)
33
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  def set_webhook():
35
  """Sets the webhook for the Telegram bot."""
36
  try:
@@ -46,28 +58,29 @@ def set_webhook():
46
  @webhook_endpoint
47
  async def handle_update(payload: WebhookPayload) -> None:
48
  """Handles incoming updates from Telegram."""
49
- logger.debug(f"Received payload: {payload}")
50
  if payload.message:
51
  message_text = payload.message.get('text')
52
  chat_id = payload.message.get('chat').get('id')
53
 
54
  if message_text:
55
  try:
56
- # Generate a response using Google Generative AI or predefined responses
57
  prompt = f"Respond to the user: {message_text}"
58
- response = requests.post(
59
- "https://api.your-ai-service.com/generate", # Replace with your AI service URL
60
- json={"prompt": prompt, "api_key": GOOGLE_API_KEY}
61
- ).json()
62
- response_text = response.get('text', 'Sorry, I didn\'t understand that.')
63
 
64
- # Send the response back to the chat
65
  bot.send_message(chat_id, response_text, parse_mode='Markdown')
66
  logger.info(f"Response sent to chat_id {chat_id}")
67
 
68
  except Exception as e:
69
- logger.error(f"Error during message processing: {e}")
70
- bot.send_message(chat_id, "Sorry, I can't answer this query right now.", parse_mode='Markdown')
 
 
71
 
72
  # Initialize the Gradio interface
73
  iface = Interface(fn=handle_update, inputs=[], outputs=[])
 
31
  )
32
  logger = logging.getLogger(__name__)
33
 
34
+ def set_webhook():
35
+ """Sets the webhook for the Telegram bot."""
36
+ try:
37
+ response = requests.get(f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook?url={WEBHOOK_URL}")
38
+ result = response.json()
39
+ if result.get('ok'):
40
+ logger.info("Webhook set successfully.")
41
+ else:
42
+ logger.error(f"Failed to set webhook: {result}")
43
+ except requests.exceptions.RequestException as e:
44
+ logger.error(f"Request exception: {e}")
45
+
46
  def set_webhook():
47
  """Sets the webhook for the Telegram bot."""
48
  try:
 
58
  @webhook_endpoint
59
  async def handle_update(payload: WebhookPayload) -> None:
60
  """Handles incoming updates from Telegram."""
 
61
  if payload.message:
62
  message_text = payload.message.get('text')
63
  chat_id = payload.message.get('chat').get('id')
64
 
65
  if message_text:
66
  try:
67
+ # Generate a response using Google Generative AI
68
  prompt = f"Respond to the user: {message_text}"
69
+ response = chat_session.send_message(prompt) # Generate response using text and prompt
70
+ response_text = response.text
71
+
72
+ # Log the response
73
+ logger.debug(f"Generated response: {response_text}")
74
 
75
+ # Send the response to the chat
76
  bot.send_message(chat_id, response_text, parse_mode='Markdown')
77
  logger.info(f"Response sent to chat_id {chat_id}")
78
 
79
  except Exception as e:
80
+ logger.error(f"Error during GenAI processing: {e}")
81
+ error_message = "Sorry, I can't answer this query right now but I will improve from time to time."
82
+ bot.send_message(chat_id, error_message, parse_mode='Markdown')
83
+ logger.error(f"Error message sent to chat_id {chat_id}")
84
 
85
  # Initialize the Gradio interface
86
  iface = Interface(fn=handle_update, inputs=[], outputs=[])