Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,90 @@
|
|
| 1 |
from flask import Flask, request
|
| 2 |
import telebot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
| 4 |
BOT_TOKEN = '7484321656:AAFaswxTqaSHu_s4jd_pk2Q2OJJWYcWHwAM'
|
|
|
|
|
|
|
| 5 |
bot = telebot.TeleBot(BOT_TOKEN)
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
@app.route(f'/bot{BOT_TOKEN}', methods=['POST'])
|
| 10 |
def webhook():
|
|
|
|
| 11 |
json_str = request.get_data(as_text=True)
|
| 12 |
update = telebot.types.Update.de_json(json_str)
|
| 13 |
bot.process_new_updates([update])
|
| 14 |
return 'ok', 200
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
if __name__ == "__main__":
|
|
|
|
| 17 |
app.run(host='0.0.0.0', port=5000)
|
|
|
|
| 1 |
from flask import Flask, request
|
| 2 |
import telebot
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
import tempfile
|
| 7 |
+
import logging
|
| 8 |
+
import re
|
| 9 |
+
from telebot.apihelper import ApiTelegramException
|
| 10 |
+
import time
|
| 11 |
|
| 12 |
+
# Replace with your actual API keys and bot token
|
| 13 |
+
GOOGLE_API_KEY = 'AIzaSyAYXUMnwmR4nUGDCs97FJJsafcQAPAAuzE'
|
| 14 |
BOT_TOKEN = '7484321656:AAFaswxTqaSHu_s4jd_pk2Q2OJJWYcWHwAM'
|
| 15 |
+
|
| 16 |
+
# Initialize the Telegram bot
|
| 17 |
bot = telebot.TeleBot(BOT_TOKEN)
|
| 18 |
|
| 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 |
+
|
| 26 |
+
# Configure Google Generative AI
|
| 27 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 28 |
+
|
| 29 |
+
# Create the model
|
| 30 |
+
generation_config = {
|
| 31 |
+
"temperature": 1,
|
| 32 |
+
"top_p": 0.95,
|
| 33 |
+
"top_k": 64,
|
| 34 |
+
"max_output_tokens": 8192,
|
| 35 |
+
"response_mime_type": "text/plain",
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
model = genai.GenerativeModel(
|
| 39 |
+
model_name="gemini-1.5-pro",
|
| 40 |
+
generation_config=generation_config,
|
| 41 |
+
system_instruction="Please respond to user input"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
chat_session = model.start_chat(
|
| 45 |
+
history=[
|
| 46 |
+
{"role": "user", "parts": ["hi\n"]},
|
| 47 |
+
{"role": "model", "parts": ["Hello! 👋 How can I help you today? 😊 \n"]},
|
| 48 |
+
{"role": "user", "parts": ["I am looking for photo booth service?"]},
|
| 49 |
+
{"role": "model", "parts": ["That's great! 🎉 I can definitely help you with information about Aforative Media's photo booth services. \n\nTo give you the most relevant information, could you tell me a little more about what you're looking for? ..."]},
|
| 50 |
+
{"role": "user", "parts": ["How much for photo booth services?"]},
|
| 51 |
+
{"role": "model", "parts": ["You're smart to ask about pricing upfront! 😉 \n\nAforative Media's Mr. & Ms. Booth photo booth services start at **USD 390 for a minimum of 2 hours**. ..."]},
|
| 52 |
+
{"role": "user", "parts": ["How about videography service?"]},
|
| 53 |
+
{"role": "model", "parts": ["You're thinking about capturing the memories on film too? Excellent choice! Videography adds a whole other dimension to remembering special events. \n\nAforative Media offers excellent videography services, and just like their photo booths, their videography packages are competitively priced and flexible. ..."]},
|
| 54 |
+
]
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Initialize Flask app
|
| 58 |
app = Flask(__name__)
|
| 59 |
|
| 60 |
@app.route(f'/bot{BOT_TOKEN}', methods=['POST'])
|
| 61 |
def webhook():
|
| 62 |
+
"""Handles incoming updates from Telegram."""
|
| 63 |
json_str = request.get_data(as_text=True)
|
| 64 |
update = telebot.types.Update.de_json(json_str)
|
| 65 |
bot.process_new_updates([update])
|
| 66 |
return 'ok', 200
|
| 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 |
+
# Determine whether to reply in private or group chat with Markdown formatting
|
| 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:
|
| 82 |
+
logger.error(f"Error during GenAI processing: {e}")
|
| 83 |
+
error_message = "Sorry, I can't answer this query right now but I will improve from time to time."
|
| 84 |
+
bot.send_message(message.chat.id, error_message, parse_mode='Markdown')
|
| 85 |
+
logger.error(f"Error message sent to chat_id {message.chat.id}")
|
| 86 |
+
|
| 87 |
+
|
| 88 |
if __name__ == "__main__":
|
| 89 |
+
set_webhook()
|
| 90 |
app.run(host='0.0.0.0', port=5000)
|