-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
350 lines (307 loc) · 14 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from gpytranslate import Translator
from os import getenv
from tiktoken import encoding_for_model
from db import DataBase
from openaitools import OpenAiTools
from stablediffusion import StableDiffusion
from cryptopay import CryptoPay
from dotenv import load_dotenv
from aiofiles.os import remove
import asyncio
from aiogram import Bot, Dispatcher, types
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.fsm.state import State, StatesGroup
from aiogram.filters.command import Command
from aiogram.fsm.context import FSMContext
from aiogram.types.input_file import FSInputFile
from aiogram import F
class States(StatesGroup):
ENTRY_STATE = State()
CHATGPT_STATE = State()
DALL_E_STATE = State()
STABLE_STATE = State()
INFO_STATE = State()
PURCHASE_STATE = State()
PURCHASE_CHATGPT_STATE = State()
PURCHASE_DALL_E_STATE = State()
PURCHASE_STABLE_STATE = State()
dp = Dispatcher()
# Starts a conversation
@dp.message(Command('start'))
@dp.message(States.ENTRY_STATE, F.text.regexp(r'^🔙Back$'))
@dp.message(States.CHATGPT_STATE, F.text.regexp(r'^🔙Back$'))
@dp.message(States.DALL_E_STATE, F.text.regexp(r'^🔙Back$'))
@dp.message(States.STABLE_STATE, F.text.regexp(r'^🔙Back$'))
@dp.message(States.INFO_STATE, F.text.regexp(r'^🔙Back$'))
async def start(message: types.Message, state: FSMContext):
user_id = message.from_user.id
username = message.from_user.username
result = await DataBase.is_user(user_id)
button = [[KeyboardButton(text="💭Chatting — ChatGPT")],
[KeyboardButton(text="🌄Image generation — DALL·E")],
[KeyboardButton(text="🌅Image generation — Stable Diffusion")],
[KeyboardButton(text="👤My account | 💰Buy")]]
reply_markup = ReplyKeyboardMarkup(
keyboard = button, resize_keyboard=True
)
if not result:
await DataBase.insert_user(user_id, username)
await message.answer(
text = "👋You have: \n💭3000 ChatGPT tokens \n🌄3 DALL·E Image Generations \n🌅3 Stable Diffusion Image generations\n Choose an option: 👇 \n If buttons don't work, enter /start command",
reply_markup=reply_markup,
)
else:
await message.answer(
text = "Choose an option: 👇🏻 \n If buttons don't work, enter /start command",
reply_markup=reply_markup,
)
await state.set_state(States.ENTRY_STATE)
# Question Handling
@dp.message(States.ENTRY_STATE, F.text.regexp(r'^💭Chatting — ChatGPT$'))
@dp.message(States.ENTRY_STATE, F.text.regexp(r'^🌄Image generation — DALL·E$'))
@dp.message(States.ENTRY_STATE, F.text.regexp(r'^🌅Image generation — Stable Diffusion$'))
async def question_handler(message: types.Message, state: FSMContext):
button = [[KeyboardButton(text="🔙Back")]]
reply_markup = ReplyKeyboardMarkup(
keyboard = button, resize_keyboard=True
)
await message.answer(
text = "Enter your text: 👇🏻",
reply_markup=reply_markup,
)
option = message.text
if option == "💭Chatting — ChatGPT":
await state.set_state(States.CHATGPT_STATE)
elif option == "🌄Image generation — DALL·E":
await state.set_state(States.DALL_E_STATE)
elif option == "🌅Image generation — Stable Diffusion":
await state.set_state(States.STABLE_STATE)
# Answer Handling
@dp.message(States.CHATGPT_STATE, F.text)
async def chatgpt_answer_handler(message: types.Message, state: FSMContext):
button = [[KeyboardButton(text="🔙Back")]]
reply_markup = ReplyKeyboardMarkup(
keyboard = button, resize_keyboard=True
)
user_id = message.from_user.id
result = await DataBase.get_chatgpt(user_id)
if result > 0:
question = message.text
answer = await OpenAiTools.get_chatgpt(question)
if answer:
await message.answer(
text = answer,
reply_markup=reply_markup,
)
result -= len(await asyncio.get_running_loop().run_in_executor(None, encoding.encode,question + answer))
if result > 0:
await DataBase.set_chatgpt(user_id, result)
else:
await DataBase.set_chatgpt(user_id, 0)
else:
await message.answer(
text = "❌Your request activated the API's safety filters and could not be processed. Please modify the prompt and try again.",
reply_markup=reply_markup,
)
else:
await message.answer(
text = "❎You have 0 ChatGPT tokens. You need to buy them to use ChatGPT.",
reply_markup=reply_markup,
)
await state.set_state(States.CHATGPT_STATE)
# Answer Handling
@dp.message(States.DALL_E_STATE, F.text)
async def dall_e_answer_handler(message: types.Message, state: FSMContext):
button = [[KeyboardButton(text="🔙Back")]]
reply_markup = ReplyKeyboardMarkup(
keyboard = button, resize_keyboard=True
)
user_id = message.from_user.id
result = await DataBase.get_dalle(user_id)
if result > 0:
question = message.text
prompt = await translator.translate(question, targetlang='en')
answer = await OpenAiTools.get_dalle(prompt.text)
if answer:
await message.answer_photo(
photo=answer,
reply_markup=reply_markup,
caption=question,
)
result -= 1
await DataBase.set_dalle(user_id, result)
else:
await message.answer(
text = "❌Your request activated the API's safety filters and could not be processed. Please modify the prompt and try again.",
reply_markup=reply_markup,
)
else:
await message.answer(
text = "❎You have 0 DALL·E image generations. You need to buy them to use DALL·E.",
reply_markup=reply_markup,
)
await state.set_state(States.DALL_E_STATE)
# Answer Handling
@dp.message(States.STABLE_STATE, F.text)
async def stable_answer_handler(message: types, state: FSMContext):
button = [[KeyboardButton(text="🔙Back")]]
reply_markup = ReplyKeyboardMarkup(
keyboard = button, resize_keyboard=True
)
user_id = message.from_user.id
result = await DataBase.get_stable(user_id)
if result > 0:
question = message.text
prompt = await translator.translate(question, targetlang='en')
path = await asyncio.get_running_loop().run_in_executor(None, StableDiffusion.get_stable,prompt.text)
if path:
await message.answer_photo(
photo=FSInputFile(path),
reply_markup=reply_markup,
caption=question,
)
await remove(path)
result -= 1
await DataBase.set_stable(user_id, result)
else:
await message.answer(
text = "❌Your request activated the API's safety filters and could not be processed. Please modify the prompt and try again.",
reply_markup=reply_markup,
)
else:
await message.answer(
text = "❎You have 0 Stable Diffusion image generations. You need to buy them to use Stable Diffusion.",
reply_markup=reply_markup,
)
await state.set_state(States.STABLE_STATE)
# Displays information about user
@dp.message(States.ENTRY_STATE, F.text.regexp(r'^👤My account | 💰Buy$'))
@dp.message(States.PURCHASE_STATE, F.text.regexp(r'^🔙Back$'))
async def display_info(message: types.Message, state: FSMContext):
user_id = message.from_user.id
result = await DataBase.get_userinfo(user_id)
button = [[KeyboardButton(text="💰Buy tokens and generations")], [KeyboardButton(text="🔙Back")]]
reply_markup = ReplyKeyboardMarkup(
keyboard = button, resize_keyboard=True
)
await message.answer(
text = f"You have: \n 💭{result[2]} ChatGPT tokens \n 🌄{result[3]} DALL·E image generations \n 🌅{result[4]} Stable Diffusion image generations \n 💸 You can buy more with crypto",
reply_markup=reply_markup,
)
await state.set_state(States.INFO_STATE)
# Displays goods
@dp.message(States.INFO_STATE, F.text.regexp(r'^💰Buy tokens and generations$'))
@dp.message(States.PURCHASE_CHATGPT_STATE, F.text.regexp(r'^🔙Back$'))
@dp.message(States.PURCHASE_DALL_E_STATE, F.text.regexp(r'^🔙Back$'))
@dp.message(States.PURCHASE_STABLE_STATE, F.text.regexp(r'^🔙Back$'))
async def purchase(message: types.Message, state: FSMContext):
button = [[KeyboardButton(text="100K ChatGPT tokens - 5 USD💵")],
[KeyboardButton(text="100 DALL·E image generations - 5 USD💵")],
[KeyboardButton(text="100 Stable Diffusion image generations - 5 USD💵")],
[KeyboardButton(text="🔙Back")]]
reply_markup = ReplyKeyboardMarkup(
keyboard = button, resize_keyboard=True
)
await message.answer(
text = "Choose product: 👇",
reply_markup=reply_markup,
)
await state.set_state(States.PURCHASE_STATE)
# Displays cryptocurrencies
@dp.message(States.PURCHASE_STATE, F.text.regexp(r'^100K ChatGPT tokens - 5 USD💵$'))
@dp.message(States.PURCHASE_STATE, F.text.regexp(r'^100 DALL·E image generations - 5 USD💵$'))
@dp.message(States.PURCHASE_STATE, F.text.regexp(r'^100 Stable Diffusion image generations - 5 USD💵$'))
async def currencies(message: types.Message, state: FSMContext):
buttons = [
[KeyboardButton(text="💲USDT"),
KeyboardButton(text="💲TON")],
[KeyboardButton(text="💲BTC"),
KeyboardButton(text="💲ETH")],
[KeyboardButton(text="🔙Back")]
]
keyboard = ReplyKeyboardMarkup(
keyboard = buttons,
resize_keyboard=True
)
await message.answer(
text = "Choose currency: 👇",
reply_markup=keyboard,
)
product = message.text
if product == "100K ChatGPT tokens - 5 USD💵":
await state.set_state(States.PURCHASE_CHATGPT_STATE)
elif product == "100 DALL·E image generations - 5 USD💵":
await state.set_state(States.PURCHASE_DALL_E_STATE)
elif product == "100 Stable Diffusion image generations - 5 USD💵":
await state.set_state(States.PURCHASE_STABLE_STATE)
# Makes invoice and displays it
@dp.message(States.PURCHASE_CHATGPT_STATE, F.text.regexp(r'^💲USDT$'))
@dp.message(States.PURCHASE_CHATGPT_STATE, F.text.regexp(r'^💲TON$'))
@dp.message(States.PURCHASE_CHATGPT_STATE, F.text.regexp(r'^💲BTC$'))
@dp.message(States.PURCHASE_CHATGPT_STATE, F.text.regexp(r'^💲ETH$'))
@dp.message(States.PURCHASE_DALL_E_STATE, F.text.regexp(r'^💲USDT$'))
@dp.message(States.PURCHASE_DALL_E_STATE, F.text.regexp(r'^💲TON$'))
@dp.message(States.PURCHASE_DALL_E_STATE, F.text.regexp(r'^💲BTC$'))
@dp.message(States.PURCHASE_DALL_E_STATE, F.text.regexp(r'^💲ETH$'))
@dp.message(States.PURCHASE_STABLE_STATE, F.text.regexp(r'^💲USDT$'))
@dp.message(States.PURCHASE_STABLE_STATE, F.text.regexp(r'^💲TON$'))
@dp.message(States.PURCHASE_STABLE_STATE, F.text.regexp(r'^💲BTC$'))
@dp.message(States.PURCHASE_STABLE_STATE, F.text.regexp(r'^💲ETH$'))
async def buy(message: types.Message, state: FSMContext):
user_id = message.from_user.id
currency = message.text
invoice_url, invoice_id = await CryptoPay.create_invoice(5, currency[1:])
current_state = await state.get_state()
product = ''
if current_state == States.PURCHASE_CHATGPT_STATE:
product = '100K ChatGPT tokens - 5 USD💵'
await DataBase.new_order(invoice_id, user_id, 'chatgpt')
elif current_state == States.PURCHASE_DALL_E_STATE:
product = '100 DALL·E image generations - 5 USD💵'
await DataBase.new_order(invoice_id, user_id, 'dall_e')
elif current_state == States.PURCHASE_STABLE_STATE:
product = '100 Stable Diffusion image generations - 5 USD💵'
await DataBase.new_order(invoice_id, user_id, 'stable')
keyboard = InlineKeyboardMarkup(
inline_keyboard = [
[InlineKeyboardButton(text="💰Buy", url=invoice_url),
InlineKeyboardButton(text="☑️Check", callback_data=str(invoice_id))],
]
)
await message.answer(
text = f"🪙Product: {product} \n 💳If you want to pay click the button 'Buy', click button 'Start' in Crypto Bot and follow the instructions \n ❗Consider the network commission \n ☑️After payment you should tap 'Check' button to check payment \n If you don't want to pay tap the 'Back' button: 👇",
reply_markup=keyboard,
)
# Checks payment
@dp.callback_query()
async def keyboard_callback(callback_query: types.CallbackQuery):
query = callback_query
invoice_id = int(query.data)
result = await DataBase.get_orderdata(invoice_id)
if result:
status = await CryptoPay.get_status(invoice_id)
if status == "active":
await query.answer("⌚️We have not received your payment yet")
elif status == "paid":
if result[1] == 'chatgpt':
await DataBase.update_chatgpt(result[0], invoice_id)
await query.answer("✅Successful payment, tokens were added to your account")
elif result[1] == 'dall_e':
await DataBase.update_dalle(result[0], invoice_id)
await query.answer("✅Successful payment, image generations were added to your account")
elif result[1] == 'stable':
await DataBase.update_stable(result[0], invoice_id)
await query.answer("✅Successful payment, image generations were added to your account")
elif status == "expired":
await query.answer("❎Payment has expired, create a new payment")
else:
await query.answer("✅You have already received your purchase")
async def main():
await DataBase.open_pool()
await dp.start_polling(bot)
if __name__ == '__main__':
load_dotenv()
translator = Translator()
bot = Bot(token=getenv("TELEGRAM_BOT_TOKEN"))
encoding = encoding_for_model("gpt-3.5-turbo")
asyncio.run(main())