def clash_enable(config, bot, update): send_typing_action(bot, update) username = update.message.from_user.username chat_id = update.message.chat_id message_id = update.message.message_id if not username: msg = "The unnamed ones are free from these silly humans vanity" else: with connector(config.engine()) as ses: all_excludes = ses.query(ClashExclude.username).filter(ClashExclude.chat_id == update.message.chat_id).all() all_excludes = [ x for x in all_excludes for x in x ] if username in all_excludes: ses.query(ClashExclude).filter(and_( ClashExclude.chat_id == chat_id, ClashExclude.username == username)).delete() msg = "You will be notified when a new game is created!" else: msg = "You've already subscribed" bot.send_message(chat_id=update.message.chat_id, reply_to_message_id=message_id, text=msg, parse_mode="markdown") log_print("Enabled", chat_id=update.message.chat_id, username=username, level="INFO", command="clash_disable")
def me(config, bot, update, args): import telegram send_typing_action(bot, update) username = update.message.from_user.username with connector(config.engine()) as ses: user_matches = ses.query(Pingers).filter(Pingers.chat_id == update.message.chat_id, Pingers.username == username).all() out_text = "" if [x.match for x in user_matches if x.me == 1]: match = random.choice([x.match for x in user_matches if x.me == 1]) else: match = username out_text=italize("{match} {message}".format( match=match.capitalize(), message=escape(' '.join(args)))) bot.send_message(chat_id=update.message.chat_id, text=out_text, parse_mode=telegram.ParseMode.HTML) bot.delete_message(chat_id=update.message.chat_id, message_id=update.message.message_id) log_print("Me", chat_id=update.message.chat_id, match=match, username=username, level="INFO", command="me")
def clash_disable(config, bot, update): send_typing_action(bot, update) username = update.message.from_user.username chat_id = update.message.chat_id message_id = update.message.message_id if not username: msg = "The unnamed ones are free from these silly humans vanity" else: with connector(config.engine()) as ses: all_excludes = ses.query(ClashExclude.username).filter(ClashExclude.chat_id == update.message.chat_id).all() all_excludes = [ x for x in all_excludes for x in x ] if username in all_excludes: msg = "You've already disabled notifications" else: exclude = ClashExclude( username=username, chat_id=chat_id) ses.add(exclude) msg = "Now you won't receive any notifications about Clash of Code games" bot.send_message(chat_id=update.message.chat_id, reply_to_message_id=message_id, text=msg) log_print("Disabled", chat_id=update.message.chat_id, username=username, level="INFO", command="clash_disable")
def chat_id(bot, update): send_typing_action(bot, update) current_chat_id = update.message.chat_id username = update.message.from_user.username bot.send_message(chat_id=current_chat_id, text="`{0}`".format(current_chat_id), reply_to_message_id=update.message.message_id, parse_mode='markdown') log_print('Chat id {0}'.format(current_chat_id), username)
def start(bot, update): send_typing_action(bot, update) start_text = ''' This is the Yet One Bot Assistant You can see the code here https://github.com/Cuttlerat/yoba by @Cuttlerat ''' start_text = "\n".join([i.strip() for i in start_text.split('\n')]) bot.send_message(chat_id=update.message.chat_id, text=start_text)
def bug(bot, update): send_typing_action(bot, update) bug_text = ''' *Found a bug?* Please report it here: https://github.com/SimonBorin/yoba/issues/new ''' bug_text = "\n".join([i.strip() for i in bug_text.split('\n')]) bot.send_message(chat_id=update.message.chat_id, text=bug_text, parse_mode='markdown')
def start(bot, update): send_typing_action(bot, update) start_text = ''' This is the Yet One Bot Assistant You can see the forked code here https://github.com/SimonBorin/yoba maintaining by @blooomberg origin by @Cuttlerat ''' start_text = "\n".join([i.strip() for i in start_text.split('\n')]) bot.send_message(chat_id=update.message.chat_id, text=start_text)
async def ping_parser(config, bot, update): in_text = prepare_message(update) with connector(config.engine()) as ses: in_text_list = in_text.split() username = update.message.from_user.username chat_id = update.message.chat_id try: ses.query(PingPhrases.phrase).filter( PingPhrases.phrase.in_(in_text_list)).limit(1).one() usernames = ses.query(Pingers.username).filter( and_(Pingers.match.in_(in_text_list), or_(Pingers.chat_id == chat_id, Pingers.chat_id == "all"))).distinct().all() usernames = [i for i in usernames for i in i] if 'EVERYONE GET IN HERE' in usernames: try: ses.query(PingExcludes.match).filter( PingExcludes.match.in_(in_text_list)).one() usernames = ses.query(Pingers.username).filter( and_( Pingers.username.notin_(usernames), Pingers.username != username, or_(Pingers.chat_id == chat_id, Pingers.chat_id == "all"))).distinct().all() usernames = [i for i in usernames for i in i] except NoResultFound: if ['EVERYONE GET IN HERE'] == usernames: usernames = ses.query(Pingers.username).filter( and_( Pingers.username != 'EVERYONE GET IN HERE', Pingers.username != username, or_(Pingers.chat_id == chat_id, Pingers.chat_id == "all"))).distinct().all() usernames = [i for i in usernames for i in i] if usernames: send_typing_action(bot, update) if 'EVERYONE GET IN HERE' in usernames: usernames.remove('EVERYONE GET IN HERE') out_text = " ".join(["@" + i for i in usernames]) bot.send_message(chat_id=update.message.chat_id, reply_to_message_id=update.message.message_id, text=out_text) log_print("Ping", chat_id=update.message.chat_id, username=username, pinged=out_text.split(), level="INFO", command="ping") except NoResultFound: pass
def clash_start(config, bot, update): send_typing_action(bot, update) username = update.message.from_user.username last_game = get_last_game(config, username, update.message.chat_id) cookies = clash_get_cookies(config) if last_game["clash_id"]: if last_game["username"] == username: r = requests.post('https://www.codingame.com/services/ClashOfCodeRemoteService/startClashByHandle', headers={"content-type":"application/json;charset=UTF-8", "cookie":"rememberMe={remember_me};cgSession={cg_session}".format( remember_me=cookies["rememberMe"], cg_session=cookies["cgSession"])}, data='[{user_id}, "{clash_id}"]'.format(user_id=cookies["user_id"], clash_id=last_game["clash_id"])) if r.status_code == 200 and "error" not in json.loads(r.text): message="The game is about to start, hurry up!" if last_game["users"]: message = '{users}\n\n{msg}'.format( users=last_game["users"], msg=message) log_print("Started", chat_id=update.message.chat_id, username=username, clash_id=last_game["clash_id"], level="INFO", command="clash_start") else: message = 'Could not start "{}" Clash game...'.format( last_game["clash_id"]) log_print("Failed on start", chat_id=update.message.chat_id, username=username, clash_id=last_game["clash_id"], level="ERROR", command="clash_start") else: last_game["message_id"] = update.message.message_id message = 'Only @{} is allowed to start the game'.format(last_game["username"]) else: last_game["clash_id"] = "None" message = "Could not find the last Clash id" if last_game["message_id"]: bot.send_message(chat_id=update.message.chat_id, reply_to_message_id=last_game["message_id"], text=message) else: bot.send_message(chat_id=update.message.chat_id, text=message)
def nsfw(bot, update): send_typing_action(bot, update) try: url = 'https://www.reddit.com/r/motorcycles/random.json' operUrl = urllib.request.urlopen(url) data = operUrl.read() jsonData = json.loads(data) postUrl = jsonData[0]["data"]["children"][0]["data"]["url"] except Exception: postUrl = 'https://www.reddit.com/r/motorcycles/' start_text = ''' Опять дрочешь? Иди работай!! Йоба на страже нравственности! ну или хотяб вот, держи. А то у вас тут моточат или где? ''' + postUrl start_text = "\n".join([i.strip() for i in start_text.split('\n')]) bot.send_message(chat_id=update.message.chat_id, text=start_text)
async def answer_parser(config, bot, update): in_text = prepare_message(update) with connector(config.engine()) as ses: out_text = ses.query(Answers.answer).filter( literal(in_text).contains(Answers.match)) for message in ["".join(i) for i in out_text]: send_typing_action(bot, update) bot.send_message(chat_id=update.message.chat_id, text=message) log_print("Answer", chat_id=update.message.chat_id, username=username, level="INFO", command="answer")
def welcome(config, bot, update): send_typing_action(bot, update) with connector(config.engine()) as ses: try: input_message = ses.query(Welcome.welcome).all() except NoResultFound: return welcome_message = "Welcome @{}!".format(update.message.new_chat_members[0].username) for line in input_message: welcome_message += "\n{}".format("".join(line)) log_print('Welcome "@{0}", added by @{1}'.format(update.message.new_chat_members[0].username, update.message.from_user.username)) bot.send_message(chat_id=update.message.chat_id, text=welcome_message)
def database_handler(config, bot, update, args): send_typing_action(bot, update) if update.message.from_user.username not in config.admins(): out_text = "You are not an administrator. The incident will be reported" command = "not an administrator" else: command = " ".join(args) if command == ".schema": command = "SELECT sql FROM sqlite_master WHERE type = 'table'" if command == ".tables": command = "SELECT name FROM sqlite_master WHERE type = 'table'" if "%%%chat_id%%%" in command: command = command.replace("%%%chat_id%%%", str(update.message.chat_id)) engine = create_engine(config.database()) connector = engine.connect() try: out_text = "\n".join([ " | ".join([str(i) for i in i]) for i in engine.execute(command).fetchall() ]) connector.close() except ResourceClosedError: out_text = command = "Successfuly" connector.close() except: out_text = command = "Bad command" connector.close() if out_text: bot.send_message(chat_id=update.message.chat_id, text=out_text) log_print(command, username=update.message.from_user.username, chat_id=update.message.chat_id, level="INFO", command="db")
def clash(config, bot, update): send_typing_action(bot, update) last_game={} username = update.message.from_user.username last_game = get_last_game(config, username, update.message.chat_id) clash_id = "" users = "" cookies = clash_get_cookies(config) if last_game["clash_id"]: r = requests.post('https://www.codingame.com/services/ClashOfCodeRemoteService/findClashReportInfoByHandle', headers={"content-type":"application/json;charset=UTF-8"}, data='[{}]'.format(last_game["clash_id"])) if r.status_code == 200: results = json.loads(r.text) if results["success"]["started"] or results["success"]["finished"]: r = requests.post('https://www.codingame.com/services/ClashOfCodeRemoteService/createPrivateClash', headers={"content-type":"application/json;charset=UTF-8", "cookie":"rememberMe={remember_me};cgSession={cg_session}".format( remember_me=cookies["rememberMe"], cg_session=cookies["cgSession"])}, data='[{user_id}, {{"SHORT":true}}]'.format(user_id=cookies["user_id"])) if r.status_code == 200: clash_id = json.loads(r.text)["success"]["publicHandle"] else: try: bot.delete_message(chat_id=update.message.chat_id, message_id=last_game["message_id"]) except telegram.error.BadRequest: pass clash_id = last_game["clash_id"] else: r = requests.post('https://www.codingame.com/services/ClashOfCodeRemoteService/createPrivateClash', headers={"content-type":"application/json;charset=UTF-8", "cookie":"rememberMe={remember_me};cgSession={cg_session}".format( remember_me=cookies["rememberMe"], cg_session=cookies["cgSession"])}, data='[{user_id}, {{"SHORT":true}}]'.format(user_id=cookies["user_id"])) if r.status_code == 200: clash_id = json.loads(r.text)["success"]["publicHandle"] if clash_id: with connector(config.engine()) as ses: all_matches = ses.query(Pingers.username).filter(Pingers.chat_id == update.message.chat_id).order_by(Pingers.username).distinct().all() exclude = ses.query(ClashExclude.username).filter(ClashExclude.chat_id == update.message.chat_id).all() users = [ x for x in all_matches if x not in exclude ] users = [ x for x in users for x in x ] out_text = "" users=" ".join(["@{}".format(user) for user in users]) message = """ Clash of Code! https://www.codingame.com/clashofcode/clash/{clash_id} {users} Please send /clash_disable if you don't want to receive these notifications """.format(clash_id=clash_id, users=users) if "clash_id" in last_game and last_game["clash_id"] != clash_id: last_game["username"] = username last_game["clash_id"] = clash_id log_print("Created", chat_id=update.message.chat_id, username=username, clash_id=clash_id, level="INFO", command="clash") else: log_print("Failed on creating", chat_id=update.message.chat_id, username=username, clash_id=clash_id, level="ERROR", command="clash") message = "Something went wrong..." sent = bot.send_message(chat_id=update.message.chat_id, text=message) last_game["users"] = users last_game["message_id"] = sent.message_id save_last_game(config, last_game, update.message.chat_id)