Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -19,7 +19,11 @@ bot = telebot.TeleBot(BOT_TOKEN)
|
|
19 |
# Configure logging
|
20 |
logging.basicConfig(
|
21 |
level=logging.DEBUG,
|
22 |
-
format='%(asctime)s - %(levelname)s - %(message)s'
|
|
|
|
|
|
|
|
|
23 |
)
|
24 |
logger = logging.getLogger(__name__)
|
25 |
|
@@ -60,22 +64,30 @@ app = Flask(__name__)
|
|
60 |
@app.route(f'/bot{BOT_TOKEN}', methods=['POST'])
|
61 |
def webhook():
|
62 |
"""Handles incoming updates from Telegram."""
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
@bot.message_handler(content_types=['text'])
|
69 |
def handle_text_messages(message):
|
70 |
"""Handles text messages and responds based on predefined questions or Generative AI."""
|
|
|
71 |
try:
|
72 |
prompt = f"Respond to the user: {message.text}"
|
73 |
response = chat_session.send_message(prompt) # Generate response using text and prompt
|
74 |
response_text = response.text
|
75 |
|
76 |
-
#
|
|
|
|
|
|
|
77 |
bot.send_message(message.chat.id, response_text, parse_mode='Markdown')
|
78 |
-
|
79 |
logger.info(f"Response sent to chat_id {message.chat.id}")
|
80 |
|
81 |
except Exception as e:
|
|
|
19 |
# Configure logging
|
20 |
logging.basicConfig(
|
21 |
level=logging.DEBUG,
|
22 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
23 |
+
handlers=[
|
24 |
+
logging.FileHandler('bot_debug.log'), # Log to a file
|
25 |
+
logging.StreamHandler() # Also log to console
|
26 |
+
]
|
27 |
)
|
28 |
logger = logging.getLogger(__name__)
|
29 |
|
|
|
64 |
@app.route(f'/bot{BOT_TOKEN}', methods=['POST'])
|
65 |
def webhook():
|
66 |
"""Handles incoming updates from Telegram."""
|
67 |
+
try:
|
68 |
+
json_str = request.get_data(as_text=True)
|
69 |
+
logger.debug(f"Received update: {json_str}")
|
70 |
+
update = telebot.types.Update.de_json(json_str)
|
71 |
+
bot.process_new_updates([update])
|
72 |
+
return 'ok', 200
|
73 |
+
except Exception as e:
|
74 |
+
logger.error(f"Error in webhook: {e}")
|
75 |
+
return 'error', 500
|
76 |
|
77 |
@bot.message_handler(content_types=['text'])
|
78 |
def handle_text_messages(message):
|
79 |
"""Handles text messages and responds based on predefined questions or Generative AI."""
|
80 |
+
logger.debug(f"Received message: {message.text} from chat_id {message.chat.id}")
|
81 |
try:
|
82 |
prompt = f"Respond to the user: {message.text}"
|
83 |
response = chat_session.send_message(prompt) # Generate response using text and prompt
|
84 |
response_text = response.text
|
85 |
|
86 |
+
# Log the response
|
87 |
+
logger.debug(f"Generated response: {response_text}")
|
88 |
+
|
89 |
+
# Send the response to the chat
|
90 |
bot.send_message(message.chat.id, response_text, parse_mode='Markdown')
|
|
|
91 |
logger.info(f"Response sent to chat_id {message.chat.id}")
|
92 |
|
93 |
except Exception as e:
|