Spaces:
Build error
Build error
Update app.py
Browse files
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
|
| 57 |
prompt = f"Respond to the user: {message_text}"
|
| 58 |
-
response =
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
# Send the response
|
| 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
|
| 70 |
-
|
|
|
|
|
|
|
| 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=[])
|