Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,110 +1,17 @@
|
|
1 |
-
import telebot
|
2 |
-
import os
|
3 |
-
import requests
|
4 |
-
import google.generativeai as genai
|
5 |
-
import tempfile
|
6 |
-
import logging
|
7 |
-
import re
|
8 |
from flask import Flask, request
|
9 |
-
|
10 |
-
from telebot.apihelper import ApiTelegramException
|
11 |
-
import time
|
12 |
|
13 |
-
# Replace with your actual API keys and bot token
|
14 |
-
GOOGLE_API_KEY = 'AIzaSyAYXUMnwmR4nUGDCs97FJJsafcQAPAAuzE'
|
15 |
BOT_TOKEN = '7484321656:AAFaswxTqaSHu_s4jd_pk2Q2OJJWYcWHwAM'
|
16 |
-
|
17 |
-
# Initialize the Telegram bot
|
18 |
bot = telebot.TeleBot(BOT_TOKEN)
|
19 |
|
20 |
-
# Configure logging
|
21 |
-
logging.basicConfig(
|
22 |
-
level=logging.DEBUG,
|
23 |
-
format='%(asctime)s - %(levelname)s - %(message)s'
|
24 |
-
)
|
25 |
-
logger = logging.getLogger(__name__)
|
26 |
-
|
27 |
-
# Configure Google Generative AI
|
28 |
-
genai.configure(api_key=GOOGLE_API_KEY)
|
29 |
-
|
30 |
-
# Create the model
|
31 |
-
generation_config = {
|
32 |
-
"temperature": 1,
|
33 |
-
"top_p": 0.95,
|
34 |
-
"top_k": 64,
|
35 |
-
"max_output_tokens": 8192,
|
36 |
-
"response_mime_type": "text/plain",
|
37 |
-
}
|
38 |
-
|
39 |
-
model = genai.GenerativeModel(
|
40 |
-
model_name="gemini-1.5-pro",
|
41 |
-
generation_config=generation_config,
|
42 |
-
system_instruction="Please respond to user input"
|
43 |
-
)
|
44 |
-
|
45 |
-
chat_session = model.start_chat(
|
46 |
-
history=[
|
47 |
-
{"role": "user", "parts": ["hi\n"]},
|
48 |
-
{"role": "model", "parts": ["Hello! 👋 How can I help you today? 😊 \n"]},
|
49 |
-
{"role": "user", "parts": ["I am looking for photo booth service?"]},
|
50 |
-
{"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? ..."]},
|
51 |
-
{"role": "user", "parts": ["How much for photo booth services?"]},
|
52 |
-
{"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**. ..."]},
|
53 |
-
{"role": "user", "parts": ["How about videography service?"]},
|
54 |
-
{"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. ..."]},
|
55 |
-
]
|
56 |
-
)
|
57 |
-
|
58 |
-
# Initialize Flask app
|
59 |
app = Flask(__name__)
|
60 |
|
61 |
-
|
62 |
-
@app.route('/' + BOT_TOKEN, methods=['POST'])
|
63 |
def webhook():
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
handle_message(update)
|
68 |
-
except ConnectionError as e:
|
69 |
-
logger.error(f"Network-related error occurred: {e}")
|
70 |
-
except ApiTelegramException as e:
|
71 |
-
logger.error(f"Telegram API error occurred: {e}")
|
72 |
-
except Exception as e:
|
73 |
-
logger.error(f"Unexpected error occurred: {e}")
|
74 |
return 'ok', 200
|
75 |
|
76 |
-
# Function to handle incoming messages
|
77 |
-
def handle_message(update):
|
78 |
-
message = update.message
|
79 |
-
try:
|
80 |
-
prompt = f"Respond to the user: {message.text}"
|
81 |
-
response = chat_session.send_message(prompt) # Generate response using text and prompt
|
82 |
-
response_text = response.text
|
83 |
-
|
84 |
-
# Determine whether to reply in private or group chat with Markdown formatting
|
85 |
-
if message.chat.type == 'private':
|
86 |
-
bot.send_message(message.chat.id, response_text, parse_mode='Markdown')
|
87 |
-
else:
|
88 |
-
bot.send_message(message.chat.id, response_text, parse_mode='Markdown')
|
89 |
-
|
90 |
-
logger.info(f"Response sent to chat_id {message.chat.id}")
|
91 |
-
|
92 |
-
except Exception as e:
|
93 |
-
logger.error(f"Error during GenAI processing: {e}")
|
94 |
-
error_message = "Sorry, I can't answer this query right now but I will improve from time to time."
|
95 |
-
|
96 |
-
if message.chat.type == 'private':
|
97 |
-
bot.send_message(message.chat.id, error_message, parse_mode='Markdown')
|
98 |
-
else:
|
99 |
-
bot.send_message(message.chat.id, error_message, parse_mode='Markdown')
|
100 |
-
|
101 |
-
logger.error(f"Error message sent to chat_id {message.chat.id}")
|
102 |
-
|
103 |
-
# Function to set the webhook with Telegram
|
104 |
-
def set_webhook():
|
105 |
-
webhook_url = 'https://measmonysuon-flyingbird.hf.space/' + BOT_TOKEN
|
106 |
-
bot.set_webhook(url=webhook_url)
|
107 |
-
|
108 |
if __name__ == "__main__":
|
109 |
-
set_webhook()
|
110 |
app.run(host='0.0.0.0', port=5000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|