-
Notifications
You must be signed in to change notification settings - Fork 10
/
bot.py
221 lines (156 loc) Β· 5.11 KB
/
bot.py
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
# v = "0.0.2"
import os
import json
import requests
import time
from copilot import Copilot
from text_to_speech import TextToSpeech
from text_to_image import TextToImage
# from speech_to_text import SpeechToText
from translate import Translator
from dotenv import load_dotenv
from telegram import (
ReplyKeyboardMarkup,
Update,
KeyboardButton,
)
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
ConversationHandler,
MessageHandler,
filters,
)
(ENTRY_STATE,
QUESTION_STATE,
AUDIO_STATE,
IMAGE_STATE, ) = range(4)
def _generate_copilot(prompt: str):
"""Gets answer from copilot"""
copilot = Copilot()
c = copilot.get_answer(prompt)
return c
def _translate(text: str):
"""Translates the text to English"""
translator = Translator()
t = translator.translate(text)
return t
def _to_speech(text: str):
"""Converts text to speech"""
tts = TextToSpeech()
p = tts.to_speech(text)
return p
def _to_image(text: str):
"""Converts text to image"""
tti = TextToImage()
i = tti.to_image(text)
return i
#Starting the bot
async def start(update: Update, context: ContextTypes):
"""Start the conversation and ask user for input."""
button = [[KeyboardButton(text="Savol javob")], [KeyboardButton(text="Rasm generatsiya")]]
reply_markup = ReplyKeyboardMarkup(
button, resize_keyboard=True
)
await update.message.reply_text(
"Tanlang: ππ»",
reply_markup=reply_markup,
)
return ENTRY_STATE
#Handling the question
async def pre_query_handler(update: Update, context: ContextTypes):
"""Ask the user for a query."""
button = [[KeyboardButton(text="Orqaga")]]
reply_markup = ReplyKeyboardMarkup(
button, resize_keyboard=True
)
await update.message.reply_text(
"Savolingizni yozing: ππ»",
reply_markup=reply_markup,
)
return QUESTION_STATE
#Handling the question
async def pre_image_handler(update: Update, context: ContextTypes):
"""Ask the user for a query."""
button = [[KeyboardButton(text="Orqaga")]]
reply_markup = ReplyKeyboardMarkup(
button, resize_keyboard=True
)
await update.message.reply_text(
"Rasmni ifodalang: ππ»",
reply_markup=reply_markup,
)
return IMAGE_STATE
#Handling the answer
async def pre_query_answer_handler(update: Update, context: ContextTypes):
"""Display the answer to the user."""
button = [[KeyboardButton(text="Orqaga")], [KeyboardButton(text="Audioni eshitish")]]
reply_markup = ReplyKeyboardMarkup(
button, resize_keyboard=True
)
question = update.message.text
answer = _generate_copilot(question)
context.user_data['answer'] = answer
await update.message.reply_text(
answer,
reply_markup=reply_markup,
)
return QUESTION_STATE
#Handling the answer
async def pre_image_answer_handler(update: Update, context: ContextTypes):
"""Display the answer to the user."""
button = [[KeyboardButton(text="Orqaga")]]
reply_markup = ReplyKeyboardMarkup(
button, resize_keyboard=True
)
question = update.message.text
print(question)
en_v = _translate(question)
print(en_v)
path = _to_image(en_v)
context.user_data['image_path'] = _to_image
await update.message.reply_photo(
photo=open(path, 'rb'),
reply_markup=reply_markup,
caption=question,
)
os.remove(path)
return IMAGE_STATE
#Handling the audio
async def pre_query_audio_handler(update: Update, context: ContextTypes):
"""Display the answer to the user."""
fp = _to_speech(context.user_data['answer'])
await update.message.reply_audio(audio=open(fp, 'rb'))
os.remove(fp)
return QUESTION_STATE
if __name__ == '__main__':
load_dotenv()
application = Application.builder().token("YOUR TOKEN").read_timeout(100).get_updates_read_timeout(100).build()
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
ENTRY_STATE: [
MessageHandler(filters.Regex('^Orqaga$'), start),
MessageHandler(filters.Regex('^Savol javob$'), pre_query_handler),
MessageHandler(filters.Regex('^Rasm generatsiya$'), pre_image_handler),
],
QUESTION_STATE: [
MessageHandler(filters.Regex('^Orqaga$'), start),
MessageHandler(filters.Regex('^Audioni eshitish$'), pre_query_audio_handler),
MessageHandler(filters.TEXT, pre_query_answer_handler),
],
AUDIO_STATE: [
MessageHandler(filters.Regex('^Orqaga$'), start),
MessageHandler(filters.TEXT, pre_query_answer_handler),
],
IMAGE_STATE: [
MessageHandler(filters.Regex('^Orqaga$'), start),
MessageHandler(filters.TEXT, pre_image_answer_handler),
],
},
fallbacks=[],
)
application.add_handler(conv_handler)
print("Bot started")
application.run_polling()