This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
323 lines (287 loc) · 16.7 KB
/
main.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
import messages
import payment
import stablediffusion
import os
import logging
import dalle2
import midjourney
# you need the python-telegram-bot prerelease (v20.0)
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters
from dotenv import load_dotenv
import qrcode
import time
from rclone.rclone import Rclone
rc = Rclone()
load_dotenv()
# logging configuration
logging.basicConfig(
filename="mylog.txt",
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
# telegram functions
user_state = {}
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.start)
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.group)
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.todo)
except:
print("Sending /start message failed")
logging.error("Sending start message failed")
async def help(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.help)
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.todo)
async def problem(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.problem)
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.todo)
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
if str(chat_id) == str(os.environ['tg_group_id']):
pass
else:
user_state[chat_id] = [update.message.text, payment.getinvoice(), False]
img = qrcode.make("lightning:" + user_state[chat_id][1]['payment_request'])
img.save(str(chat_id) + ".png")
try:
await context.bot.send_photo(chat_id=chat_id,
photo=open(str(chat_id) + ".png", 'rb'))
os.remove(str(chat_id) + ".png")
await context.bot.send_message(chat_id=chat_id,
text="`lightning:" + user_state[chat_id][1][
'payment_request'] + "`",
parse_mode='MarkdownV2')
await context.bot.send_message(chat_id=chat_id, text="Press \n/generate_dalle2 \nor \n "
"/generate_stablediffusion \nor \n"
"/generate_midjourney (experimental MJ model, 2 pics) \nonce you "
"paid the invoice")
except:
logging.error("Answer to command failed")
print("Answer to command failed")
async def group_prompt(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
user_state[chat_id] = [update.message.text[8:], payment.getinvoice(), False]
img = qrcode.make("lightning:" + user_state[chat_id][1]['payment_request'])
img.save(str(chat_id) + ".png")
try:
await context.bot.send_photo(chat_id=chat_id,
photo=open(str(chat_id) + ".png", 'rb'))
os.remove(str(chat_id) + ".png")
await context.bot.send_message(chat_id=chat_id,
text="`lightning:" + user_state[chat_id][1][
'payment_request'] + "`",
parse_mode='MarkdownV2')
await context.bot.send_message(chat_id=chat_id, text="Press \n/generate_dalle2 \nor \n "
"/generate_stablediffusion \nor \n"
"/generate_midjourney (experimental, 2 pics)\nonce you "
"paid the invoice")
except:
logging.error("Answer to command failed")
print("Answer to command failed")
async def paid_dalle(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
if chat_id in user_state:
if payment.checkinvoice(user_state[chat_id][1]['payment_hash']):
await context.bot.send_message(chat_id=chat_id,
text="Generating pictures, this will take around 1 minute..")
for generating in range(2):
try:
file_paths = dalle2.generate_and_download(user_state[chat_id][0])
if isinstance(file_paths, list):
for n in file_paths:
await context.bot.send_photo(chat_id=chat_id, photo=open(n, 'rb'))
os.remove(n)
user_state.pop(chat_id)
elif file_paths == "violation":
logging.info("dalle violation: " + user_state[chat_id][0])
await context.bot.send_message(chat_id=chat_id,
text=messages.violation)
user_state.pop(chat_id)
elif file_paths == "failure":
logging.error(user_state[chat_id][0])
await context.bot.send_message(chat_id=chat_id,
text="This request failed due to some problems with the DALLE2 "
"API.")
await context.bot.send_message(chat_id=chat_id,
text="You can use /refund lnbc...(invoice) with a 1000 "
"Satoshi invoice to get a refund.")
user_state[chat_id][2] = True
break
except:
logging.error(user_state[chat_id][0])
await context.bot.send_message(chat_id=chat_id,
text="Failed, trying again. If it doesn't give you pictures in a "
"minute click /problem")
time.sleep(15)
else:
await context.bot.send_message(chat_id=chat_id,
text="You haven't paid, press \n/generate_dalle2 again once you paid the "
"invoice")
else:
pass
async def paid_stablediffusion(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
if chat_id in user_state:
if payment.checkinvoice(user_state[chat_id][1]['payment_hash']):
await context.bot.send_message(chat_id=chat_id,
text="Generating pictures, this will take around 1 minute..")
for generating in range(2):
if stablediffusion.find_seed(user_state[chat_id][0]) == "seed_too_long":
await context.bot.send_message(chat_id=chat_id,
text="Seed too long, use max. 9 digits, generating without seed "
"now.")
elif stablediffusion.find_seed(user_state[chat_id][0]) == "seed_no_int":
await context.bot.send_message(chat_id=chat_id,
text="Seed no integer (number), generating without seed now.")
try:
stablediffusion.generate_sd_normal(user_state[chat_id][0], str(chat_id))
for guidance in range(7, 11):
await context.bot.send_photo(chat_id=chat_id, photo=open(
'sd_picture_gd_' + str(guidance) + "_" + str(chat_id) + '.png', 'rb'))
os.remove('sd_picture_gd_' + str(guidance) + "_" + str(chat_id) + '.png')
time.sleep(1)
logging.info('sd: ' + user_state[chat_id][0])
user_state.pop(chat_id)
break
except:
if generating == 0:
logging.error("sd error: " + user_state[chat_id][0])
await context.bot.send_message(chat_id=chat_id,
text="Failed, trying again. If it doesn't give you pictures in a "
"minute click /problem")
time.sleep(15)
elif generating == 1:
logging.error(user_state[chat_id][0])
await context.bot.send_message(chat_id=chat_id,
text="This request failed due to some problems with the SD "
"API.")
await context.bot.send_message(chat_id=chat_id,
text="You can use /refund lnbc...(invoice) with a 1000 "
"Satoshi invoice to get a refund.")
user_state[chat_id][2] = True
else:
await context.bot.send_message(chat_id=update.effective_chat.id,
text="You haven't paid, press \n/generate_stablediffusion again once you "
"paid the invoice")
else:
pass
async def paid_midjourney(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
if chat_id in user_state:
if payment.checkinvoice(user_state[chat_id][1]['payment_hash']):
await context.bot.send_message(chat_id=chat_id,
text="Generating pictures, this will take around 1 minute..\n"
"Pictures appear black if the AI interprets your prompt as NSFW")
try:
images = midjourney.generate_mj(user_state[chat_id][0], str(chat_id))
if images == "failure":
logging.error(user_state[chat_id][0])
await context.bot.send_message(chat_id=chat_id,
text="This request failed due to some problems with the MJ "
"API.")
await context.bot.send_message(chat_id=chat_id,
text="You can use /refund lnbc...(invoice) with a 1000 "
"Satoshi invoice to get a refund.")
user_state[chat_id][2] = True
else:
for file in images:
await context.bot.send_photo(chat_id=chat_id, photo=open(file, 'rb'))
os.remove(file)
time.sleep(1)
logging.info('mj_tg: ' + user_state[chat_id][0])
user_state.pop(chat_id)
except:
logging.error(user_state[chat_id][0])
await context.bot.send_message(chat_id=chat_id,
text="This request failed due to some problems with the MJ "
"API.")
await context.bot.send_message(chat_id=chat_id,
text="You can use /refund lnbc...(invoice) with a 1000 "
"Satoshi invoice to get a refund.")
user_state[chat_id][2] = True
else:
await context.bot.send_message(chat_id=update.effective_chat.id,
text="You haven't paid, press \n/generate_stablediffusion again once you "
"paid the invoice")
else:
pass
async def terms(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.terms)
async def source(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.source)
async def refund(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
try:
if user_state[chat_id][2] != True:
await context.bot.send_message(chat_id=chat_id, text="You are not supposed to get a refund, if you are and "
"this is an error contact @f321x")
logging.info("!!! Somebody tried to refund without True")
elif user_state[chat_id][2] == True:
await context.bot.send_message(chat_id=chat_id, text="Trying to refund...")
refund = payment.refund(update.message.text[8:])
if refund == "success":
user_state.pop(chat_id)
await context.bot.send_message(chat_id=chat_id, text="Refund successful!")
elif refund == "wrong":
await context.bot.send_message(chat_id=chat_id, text="Your invoice is invalid, please try again with a "
"1000 Satoshi invoice. Use the format /refund "
"bolt11Invoice")
elif refund == "error":
await context.bot.send_message(chat_id=chat_id, text="Refunding failed, please try again with an "
"invoice from another Wallet")
except:
await context.bot.send_message(chat_id=chat_id, text="You are not supposed to get a refund, if you are and "
"this is an error contact @f321x")
logging.info("!!! Somebody tried to refund without True")
async def logs(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
logging.info("!!! Somebody used /logs !!!")
if update.message.text[6:] == os.environ['log_pw']:
log_file = open('mylog.txt', 'rb')
await context.bot.send_document(chat_id, log_file)
log_file.close()
else:
await context.bot.send_message(chat_id=update.effective_chat.id,
text="Sorry, I didn't understand that command. Please try /start or /help")
time.sleep(3)
async def unknown(update: Update, context: ContextTypes.DEFAULT_TYPE):
if str(update.effective_chat.id) == str(os.environ['tg_group_id']):
pass
else:
await context.bot.send_message(chat_id=update.effective_chat.id,
text="Sorry, I didn't understand that command. Please try /start or /help")
if __name__ == '__main__':
# initialize telegram bot with botfather api token
application = ApplicationBuilder().token(os.environ['telegram_token']).build()
# handlers
start_handler = CommandHandler('start', start)
help_handler = CommandHandler('help', help)
echo_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), echo)
group_handler = CommandHandler('prompt', group_prompt)
payment_handler_dalle = CommandHandler('generate_dalle2', paid_dalle)
payment_handler_sd = CommandHandler('generate_stablediffusion', paid_stablediffusion)
payment_handler_mj = CommandHandler('generate_midjourney', paid_midjourney)
term_handler = CommandHandler('terms', terms)
problem_handler = CommandHandler('problem', problem)
source_handler = CommandHandler('source', source)
refund_handler = CommandHandler('refund', refund)
log_handler = CommandHandler('logs', logs)
# enable handlers
application.add_handler(start_handler)
application.add_handler(help_handler)
application.add_handler(echo_handler)
application.add_handler(group_handler)
application.add_handler(payment_handler_dalle)
application.add_handler(payment_handler_sd)
application.add_handler(payment_handler_mj)
application.add_handler(term_handler)
application.add_handler(problem_handler)
application.add_handler(source_handler)
application.add_handler(refund_handler)
application.add_handler(log_handler)
# unknown handler
unknown_handler = MessageHandler(filters.COMMAND, unknown)
application.add_handler(unknown_handler)
# run telegram bot
application.run_polling()