-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTelegramBot.py
More file actions
107 lines (94 loc) · 4.97 KB
/
TelegramBot.py
File metadata and controls
107 lines (94 loc) · 4.97 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
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram import ChatAction
import sys, logging, json, os, io
from py573jp.EALink import EALink, EALinkException
updater = Updater(token=sys.argv[1], use_context=True)
dispatcher = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
linked_accounts = {}
def save_accounts():
global linked_accounts
js = json.dumps(linked_accounts)
with open("tg_linked.json", 'w') as f:
f.write(js)
def load_accounts():
global linked_accounts
if os.path.exists("tg_linked.json"):
with open("tg_linked.json", 'r') as f:
txt = f.read()
linked_accounts = json.loads(txt)
def start(update, context):
context.bot.send_message(chat_id=update.message.chat_id, parse_mode='Markdown', text="Hello! I'm bemani screenshot bot! I can help you with your e-amusement screenshots\n\n"
"*Commands*\n/start - This text!\n"
"/link - Link your e-amusement account\n"
"/screenshots - See your unexpired Bemani Screenshots\n"
"\nBot made by @CyberKitsune")
def link(update, context):
global linked_accounts
print("%s is linking their account!" % update.message.from_user.username)
args = update.message.text.split(" ")
if len(args) < 3:
context.bot.send_message(chat_id=update.message.chat_id, parse_mode='Markdown', text="Use this command to link your e-amusement account!\n"
"Usage:\n"
"```link [username] [password] [OTP (optional)]```")
print("%s didn't link..." % update.message.from_user.username)
return
username = args[1]
password = args[2]
otp = None
if len(args) == 4:
otp = args[3]
eal = EALink()
try:
eal.login(username, password, otp)
except EALinkException as ex:
context.bot.send_message(chat_id=update.message.chat_id, parse_mode='Markdown', text="An error occured linking your account! Details:\n```%s```" % ex)
if eal.logged_in:
linked_accounts[update.message.from_user.username] = (eal.cookies[0], eal.cookies[1])
context.bot.send_message(chat_id=update.message.chat_id, text="Successfully logged in!")
print("%s successfully linked!" % update.message.from_user.username)
save_accounts()
else:
context.bot.send_message(chat_id=update.message.chat_id, text="Couldn't log in!")
print("%s had login issues!" % update.message.from_user.username)
def screenshots(update, context):
global linked_accounts
print("%s requested screenshots!" % update.message.from_user.username)
if update.message.from_user.username not in linked_accounts:
context.bot.send_message(chat_id=update.message.chat_id, text="You're not logged in! Use /link to link your e-amusement account!")
print("%s wasn't logged in!" % update.message.from_user.username)
return
eal = EALink(cookies=(linked_accounts[update.message.from_user.username][0],linked_accounts[update.message.from_user.username][1]))
shots = None
try:
shots = eal.get_screenshot_list()
except EALinkException as ex:
context.bot.send_message(chat_id=update.message.chat_id, parse_mode='Markdown',
text="An error occured getting screenshots! Details:\n```%s```" % ex)
return
if len(shots) == 0:
context.bot.send_message(chat_id=update.message.chat_id,
text="You have no unexpired screenshots right now! Go out and get some scores!")
print("%s has no screenshots!" % update.message.from_user.username)
return
context.bot.send_message(chat_id=update.message.chat_id, text="Fetching your %i screenshots..." % len(shots))
print("%s is downloading %i screenshots" % (update.message.from_user.username, len(shots)))
context.bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.UPLOAD_PHOTO)
photo_datas = []
for photo in shots:
data = eal.get_jpeg_data_for(photo['file_path'])
photo_datas.append(data)
for data in photo_datas:
context.bot.send_photo(chat_id=update.message.chat_id, photo=io.BytesIO(data))
start_handler = CommandHandler('start', start)
link_handler = CommandHandler('link', link)
screenshot_handler = CommandHandler('screenshots', screenshots)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(link_handler)
dispatcher.add_handler(screenshot_handler)
if __name__ == "__main__":
load_accounts()
print("Launching bot...")
updater.start_polling()