-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
248 lines (210 loc) · 8.7 KB
/
bot.py
File metadata and controls
248 lines (210 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import logging
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, Voice
from telegram.constants import ParseMode
from telegram.ext import (
Application,
CallbackQueryHandler,
ContextTypes,
PicklePersistence,
MessageHandler,
CommandHandler,
)
import asyncio
import telegram.ext.filters as filters
from aiAnswer import generateAnswer
from whisper import generateAudio, generateTranscribe
import os
import dotenv
import requests
import json
dotenv.load_dotenv()
TOKEN_TELEGRAM = os.getenv("TG_TOKEN")
# Constants
certification = "<b>Made with ❤️ by HighTech</b>"
helpText = f"""
This is a simple telegram bot.
<b>Commands:</b>\n
🔍 /ask: Get instant answers to your equipment and project questions.\n
🎬 /voice: Get voice responses to your queries with clear audio.\n
📝 /help: Unsure what to do? Use the /help command to see a list of available commands.\n
<b>How to Use:</b>\n
<b>For Equipment Recommendations:</b> Simply use /ask followed by your query\n
e.g., /ask what's your name?\n
<b>For Voice Queries:</b> Activate voice responses with /voice followed by your question\n
e.g., /voice what's your role?\n
<b>For General Assistance:</b> Need guidance? Type /help to see how I can assist you further.\n
"""
homepageBtn = InlineKeyboardButton(
text="🌎Website", url="https://telegram-miniapp-three.vercel.app/"
)
docsBtn = InlineKeyboardButton(text="📜Help", callback_data="docs Btn")
keyboard_markup = InlineKeyboardMarkup(
[
[homepageBtn, docsBtn], # This is a row with two buttons
]
)
# Logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
async def get_gpt4_response(prompt):
answer = generateAnswer(prompt)
return f"{answer}"
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
userName = update.message.chat.username
userId = str(update.message.chat.id)
storage_file_path = "thread_storage.json"
# Initialize an empty dictionary to store threadIds
thread_ids = {}
# Check if the storage file exists and read the threadIds if it does
if os.path.exists(storage_file_path):
with open(storage_file_path, "r") as file:
thread_ids = json.load(file)
else:
with open(storage_file_path, "w") as file:
json.dump(thread_ids, file)
all_keys = list(thread_ids.keys())
# Check if the specific user_id exists in the dictionary
if userId in all_keys:
pass
else:
thread_ids[userId] = userName
# Save the updated dictionary to the storage file
with open(storage_file_path, "w") as file:
json.dump(thread_ids, file)
answer = f"""
🚀 <b>Welcome to @{userName} !</b>\n
{helpText}
"""
response_text = f"{answer}\n{certification}"
await update.message.reply_html(response_text, reply_markup=keyboard_markup)
async def ask(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_message = update.message.text.removeprefix("/ask").strip()
if user_message != "":
thinking_message = await update.message.reply_text("🤖 typing...")
# Await the response from GPT-4
answer = await get_gpt4_response(user_message)
response_text = f"{answer}\n\n{certification}"
await thinking_message.edit_text(
text=response_text, reply_markup=keyboard_markup, parse_mode=ParseMode.HTML
)
else:
answer = """
<b>🪫 Prompt is empty. Please provide a prompt.</b>\n
for example: <code>/ask What's your name?</code>
"""
response_text = f"{answer}\n\n{certification}"
await update.message.reply_html(response_text, reply_markup=keyboard_markup)
async def voice(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_message = update.message.text.removeprefix("/voice").strip()
if user_message != "":
thinking_message = await update.message.reply_text("🤖 typing...")
## Generate a response using GPT-4
response_text = await get_gpt4_response(user_message)
audioPath = generateAudio(response_text)
answer = "Gpt Answer"
response_text = f"{answer}\n\n{certification}"
#
await context.bot.delete_message(
chat_id=thinking_message.chat_id, message_id=thinking_message.message_id
)
# Create an InputMediaAudio object with the audio file
await update.message.reply_audio(f"{audioPath}", reply_markup=keyboard_markup)
# Remove the audio file after sending
try:
os.remove(audioPath)
# print(f"Audio file {audioPath} has been removed.")
except FileNotFoundError:
print(f"Audio file {audioPath} not found.")
except Exception as e:
print(f"Error removing audio file {audioPath}: {e}")
else:
answer = """
<b>🪫 Prompt is empty. Please provide a prompt.</b>\n
For example: <code>/voice What's your role?</code>
"""
response_text = f"{answer}\n\n{certification}"
await update.message.reply_html(response_text, reply_markup=keyboard_markup)
async def help(update: Update, context: ContextTypes.DEFAULT_TYPE):
answer = helpText
response_text = f"{answer}\n{certification}"
await update.message.reply_html(response_text, reply_markup=keyboard_markup)
async def helpBtn(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle the button click."""
query = update.callback_query
await query.answer() # Acknowledge the button click
# Extract the callback data from the clicked button
callback_data = query.data
answer = helpText
response_text = f"{answer}\n{certification}"
await query.message.reply_html(response_text, reply_markup=keyboard_markup)
async def handle_voice(update: Update, context: ContextTypes.DEFAULT_TYPE):
save_path = "userVoiceMessage.mp3"
voiceOver = ""
# Check if the message has a voice attribute
if update.message.voice:
thinking_message = await update.message.reply_text("🤖 typing...")
voice_message = update.message.voice
# Get the file ID of the voice message
file_id = voice_message.file_id
print(f"Received voice message with file ID: {file_id}")
# Get the File object
file = await context.bot.get_file(file_id)
file_path = file.file_path
print(f"File path: {file_path}")
response = requests.get(file_path)
# Send a GET request to the URL
# Check if the request was successful
if response.status_code == 200:
# Specify the path where you want to save the file
with open(save_path, "wb") as file:
file.write(response.content)
print(f"Audio file downloaded and saved to: {save_path}")
else:
print("Failed to download the file.")
# Send a reply to the user
try:
userMessage = generateTranscribe(save_path)
voiceOver = generateAnswer(userMessage)
os.remove(save_path)
print(f"Audio file {save_path} has been removed.")
except FileNotFoundError:
print(f"Audio file {save_path} not found.")
pass
except Exception as e:
print(f"Error removing audio file {save_path}: {e}")
pass
audioPath = generateAudio(voiceOver)
await context.bot.delete_message(
chat_id=thinking_message.chat_id, message_id=thinking_message.message_id
)
await update.message.reply_audio(f"{audioPath}", reply_markup=keyboard_markup)
else:
await update.message.reply_text("This is not a voice message.")
def main() -> None:
"""Run the bot."""
# We use persistence to demonstrate how buttons can still work after the bot was restarted
persistence = PicklePersistence(filepath="arbitrarycallbackdatabot")
# Create the Application and pass it your bot's token.
application = (
Application.builder()
.token(TOKEN_TELEGRAM)
.persistence(persistence)
.arbitrary_callback_data(True)
.build()
)
application.add_handler(
MessageHandler(filters._Voice and ~filters.COMMAND, handle_voice)
)
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("ask", ask))
application.add_handler(CommandHandler("voice", voice))
application.add_handler(CommandHandler("help", help))
application.add_handler(CallbackQueryHandler(helpBtn))
# Run the bot until the user presses Ctrl-C
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()