def deelgebied_updates(bot, update): """ :param bot: :param update: :return: """ chat_id = update.message.chat_id user_id = update.message.from_user.id try: State(bot, chat_id, user_id, deelgebied_conversation, change_dg_updates) message = "Voor welk deelgebied moeten updates aan of uit staan?\n " \ "A, B, C, D, E, F, X" bot.sendMessage(chat_id, message, reply_to_message_id=update.message.message_id) # TODO add a keyboard Updates.get_updates().botan.track(update.message, 'deelgebied') except MultipleConversationsError: bot.sendMessage(chat_id, "Er is al een commando actief je kunt dit " "commando niet starten.\n" " type /cancel om het vorige commando te " "stoppen te stoppen", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'incorrect_deelgebied')
def nieuws_updates(bot, update): """ :param bot: :param update: :return: """ chat_id = update.message.chat_id user_id = update.message.from_user.id try: State(bot, chat_id, user_id, add_nieuws_listener_conversation, change_niews_updates) bot.sendMessage(chat_id, "Moeten nieuws updates aan of uit staan?\n " "aan/uit", reply_to_message_id=update.message.message_id) # TODO add a keyboard Updates.get_updates().botan.track(update.message, 'nieuws_update') except MultipleConversationsError: bot.sendMessage(chat_id, "Er is al een commando actief je kunt dit " "commando niet starten.\n" " type /cancel om het vorige commando te " "stoppen te stoppen", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'incorrect_nieuws_update')
def help_command(bot: telegram.Bot, update: telegram.Update): message = start_message + "\n\n" for commando in CommandHandlerWithHelp.helps: message += '/' + commando + ' - ' + CommandHandlerWithHelp.helps[ commando] + '\n' bot.sendMessage(update.message.chat_id, message) Updates.get_updates().botan.track(update.message, 'help')
def help_command(bot, update): message = start_message + "\n\n" for commando in CommandHandlerWithHelp.helps: message += '/' + commando + ' - ' + CommandHandlerWithHelp.helps[ commando] + '\n' bot.sendMessage(update.message.chat_id, message) Updates.get_updates().botan.track(update.message, 'help')
def bug(bot: telegram.Bot, update: telegram.Update): """ :param bot: :param update: :return: """ chat_id = update.message.chat_id user_id = update.message.from_user.id try: state = State(bot, chat_id, user_id, bug_conversation, bug_done) state['command'] = update.message.text state['from'] = update.message.from_user.name keyboard = telegram.ReplyKeyboardMarkup([['app'], ['site'], ['anders']]) bot.sendMessage(chat_id, "Waar wil je een tip of een top voor " "sturen?\napp/bot/site/anders", reply_to_message_id=update.message.message_id, reply_markup=keyboard) Updates.get_updates().botan.track(update.message, 'bug') except MultipleConversationsError: bot.sendMessage(chat_id, "Er is al een commando actief je kunt dit " "commando niet starten.\n" " type /cancel om het vorige commando te " "stoppen te stoppen", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'incorrect_bug')
def set_phpsessid(bot: telegram.Bot, update: telegram.Update): chat_id = update.message.chat_id user_id = update.message.from_user.id try: state = State(bot, chat_id, user_id, phpsessid_conversation, phpsessid_done) state['command'] = update.message.text state['from'] = update.message.from_user.name message = 'stap 1: login op jotihunt.net\n ' \ 'stap2: zoek uit hoe je cookies van jothunt.net uitleest in je browser. \n ' \ 'stap 3: ga op zook naar de cookie met de naam PHPSESSID.\n ' \ 'stap 4: plak de waarde hier om de cookie te verversen van de bot.\n' \ ' of /cancel om te stoppen' bot.sendMessage(chat_id, message, reply_to_message_id=update.message.message_id) # TODO add a keyboard Updates.get_updates().botan.track(update.message, 'phpsessid') except MultipleConversationsError: bot.sendMessage(chat_id, "Er is al een commando actief je kunt dit " "commando niet starten.\n" " type /cancel om het vorige commando te " "stoppen te stoppeny", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'incorrect_phpsessid')
def bug(bot, update): """ :param bot: :param update: :return: """ chat_id = update.message.chat_id user_id = update.message.from_user.id try: state = State(bot, chat_id, user_id, bug_conversation, bug_done) state['command'] = update.message.text state['from'] = update.message.from_user.name bot.sendMessage(chat_id, "Waar wil je een tip of een top voor " "sturen?\napp/bot/site/anders", reply_to_message_id=update.message.message_id) # TODO add a keyboard Updates.get_updates().botan.track(update.message, 'bug') except MultipleConversationsError: bot.sendMessage(chat_id, "Er is al een commando actief je kunt dit " "commando niet starten.\n" " type /cancel om het vorige commando te " "stoppen te stoppen", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'incorrect_bug')
def set_phpsessid(bot, update): chat_id = update.message.chat_id user_id = update.message.from_user.id try: state = State(bot, chat_id, user_id, phpsessid_conversation, phpsessid_done) state['command'] = update.message.text state['from'] = update.message.from_user.name message = 'stap 1: login op jotihunt.net\n stap2: zoek uit hoe je ' \ 'cookies van jothunt.net uitleest in je browser. \n stap ' \ '3: ga op zook naar de cookie met de naam PHPSESSID.\n ' \ 'stap 4: plak de waarde hier om de cookie te verversen ' \ 'van de bot.\n of /cancel om te stoppen' bot.sendMessage(chat_id, message, reply_to_message_id=update.message.message_id) # TODO add a keyboard Updates.get_updates().botan.track(update.message, 'phpsessid') except MultipleConversationsError: bot.sendMessage(chat_id, "Er is al een commando actief je kunt dit " "commando niet starten.\n" " type /cancel om het vorige commando te " "stoppen te stoppen", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'incorrect_phpsessid')
def cancel(bot: telegram.Bot, update: telegram.Update): """ :param bot: :param update: :return: """ chat_id = update.message.chat_id user_id = update.message.from_user.id h = str(user_id) + str(chat_id) try: state = State.states[h] state.cancel() except KeyError: kb = telegram.ReplyKeyboardHide() if update.message.chat_id > 0: keyboard = DEFAULT_KEYBOARD kb = telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=False, selective=True) bot.sendMessage(chat_id, "Er is geen commando actief.", reply_to_message_id=update.message.message_id, reply_markup=kb) Updates.get_updates().botan.track(update.message, 'incorrect_cancel') else: kb = telegram.ReplyKeyboardHide() if update.message.chat_id > 0: keyboard = DEFAULT_KEYBOARD kb = telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=False, selective=True) bot.sendMessage(update.message.chat_id, "Het commando is gestopt.", reply_to_message_id=update.message.message_id, reply_markup=kb) Updates.get_updates().botan.track(update.message, 'cancel')
def test(bot: telegram.Bot, update: telegram.Update): bot.sendMessage(update.message.chat_id, 'de bot is online') if update.message.chat_id > 0: url = Updates.get_updates().botan.shorten('http://google.com', update.message.from_user.id) else: url = 'http://google.com' bot.sendMessage(update.message.chat_id, url) Updates.get_updates().botan.track(update.message, 'test')
def start(bot: telegram.Bot, update: telegram.Update): if update.message.chat_id > 0: keyboard = telegram.ReplyKeyboardMarkup(DEFAULT_KEYBOARD, one_time_keyboard=False) bot.sendMessage(update.message.chat_id, start_message, reply_markup=keyboard) else: bot.sendMessage(update.message.chat_id, start_message) x = authenticate() x(lambda bot2, update2: print('authenticated:\n' + str(update.to_dict( ))))(bot, update) Updates.get_updates().botan.track(update.message, 'start')
def main(): updater = create_updater() t = StoppableThread() t.start() updater.start_polling() updates = Updates.get_updates() updates.add_bot(updater.bot) updates.to_all('De bot is weer opgestart') updater.idle() t.stop() updates = Updates.get_updates() updates.to_all('de bot gaat afsluiten.') updates.error(Exception("de bot gaat stoppen"), "de bot gaat stoppen") updates.save() t.join()
def bug_conversation(bot, update, state): s = state.get_state() if s == 0: state['about'] = update.message.text message = 'stuur nu je bug, feature, tip of top.' state.next_state() elif s == 1: state['message'] = update.message.text state.done() message = 'Je input is opgeslagen en doorgestuurd naar Bram Micky ' \ 'en Mattijn (en evt. anderen).' else: message = ' dit is een fout in de bot graag aan mattijn laten weten,' \ 'bug_conversation' Updates.get_updates().error(Exception(message), 'bug_conversation') bot.sendMessage(update.message.chat_id, message)
def restart(bot, update): api = RpApi.get_instance(settings.Settings().rp_username, settings.Settings().rp_pass) response = api.get_telegram_link(update.effective_user.id) rp_acc = response.data if rp_acc is None: bot.send_message(update.effective_chat.id, 'Je telegram account is nog niet gelinkt.' ' vraag aan de homebase of ze dat willen doen.', reply_to_message_id=update.effective_message.message_id) return if int(rp_acc['toegangslvl']) < 75 and int(update.effective_user.id) != 19594180: bot.send_message(update.effective_chat.id, 'Je bent niet gemachtigd om dit commando uit te voeren', reply_to_message_id=update.effective_message.message_id) return bot.send_message(update.effective_chat.id, "bot gaat herstarten.") try: updates: Updates = Updates.get_updates() updates.to_all('de bot gaat herstarten') updates.exit() updates.save() with open(STARTUPFILE, 'wb') as file: pickle.dump({'command': 'restart'}, file) os.execl(sys.executable, sys.executable, *sys.argv) except Exception as e: print(e) raise e
def _updates_aan_uit_menu(self, update: Update, callback_query: str, rp_acc)->Tuple[str, List[InlineKeyboardButton]]: updates = Updates.get_updates() message = '' zet_aan = callback_query == 'a' if zet_aan: message = 'updates voor ' + str(self.path[-2]) + ' zijn aangezet.' else: message = 'updates voor ' + str(self.path[-2]) + ' zijn uitgezet.' if self.path[-2] == 'A': updates.set_updates(update.effective_chat.id, Updates.ALPHA, zet_aan) elif self.path[-2] == 'B': updates.set_updates(update.effective_chat.id, Updates.BRAVO, zet_aan) elif self.path[-2] == 'C': updates.set_updates(update.effective_chat.id, Updates.CHARLIE, zet_aan) elif self.path[-2] == 'D': updates.set_updates(update.effective_chat.id, Updates.DELTA, zet_aan) elif self.path[-2] == 'E': updates.set_updates(update.effective_chat.id, Updates.ECHO, zet_aan) elif self.path[-2] == 'F': updates.set_updates(update.effective_chat.id, Updates.FOXTROT, zet_aan) elif self.path[-2] == 'X': updates.set_updates(update.effective_chat.id, Updates.XRAY, zet_aan) elif self.path[-2] == 'hints': updates.set_updates(update.effective_chat.id, Updates.HINTS, zet_aan) elif self.path[-2] == 'nieuws': updates.set_updates(update.effective_chat.id, Updates.NIEUWS, zet_aan) elif self.path[-2] == 'opdracht': updates.set_updates(update.effective_chat.id, Updates.OPDRACHTEN, zet_aan) elif self.path[-2] == 'error': updates.set_updates(update.effective_chat.id, Updates.ERROR, zet_aan) else: message = 'error, waarschijnlijk heb je meerdere knoppen in het zelfde menu ingedrukt.\n' \ '_updates_aan_uit_menu, ' + str(callback_query) return message, []
def _bug_menu(self, update: Update, callback_query: str, rp_acc)->Tuple[str, List[InlineKeyboardButton]]: updates = Updates.get_updates() message = 'Er is bug gemeld.\n door: {van}\n aangeroepen met: ' \ '{command}\n het gaat over: {about}\n de text:\n {message}' message = message.format(van=update.effective_user.name, command='bug menu', about=self.path[-1], message='{message}') updates.error(Exception(message), 'bug_done') return 'er is gemeld dat je een bug hebt', []
def phpsessid_done(state): s = settings.Settings() u = Updates.get_updates() m = 'de phphsessid is aangepast van {old} naar {new}' u.error(Exception(m.format(old=str(s.phpsessid), new=str(state[ 'cookie']))), 'php_sess_id') s.phpsessid = state['cookie']
def bug_done(state): updates = Updates.get_updates() message = 'Er is bug gemeld.\n door: {van}\n aangeroepen met: '\ '{command}\n het gaat over: {about}\n de text:\n {message}' message = message.format(van=state['from'], command=state['command'], about=state['about'], message=state['message']) updates.error(Exception(message), 'bug_done') updates.bot.sendMessage(-158130982, message)
def get_opdrachten(): try: with requests.Session() as session: session.cookies.set('PHPSESSID', settings.Settings().phpsessid) r = session.get('http://www.jotihunt.net/groep/opdrachten.php') scraper = webscraper.TableHTMLParser() scraper.feed(r.text) scraper.fix_tables() # scraper.print_tables() with open('opdracht.log', 'w') as f: f.write(r.text) scraper.tables[0] = fix_opdrachten(scraper.tables[0]) return (scraper.tables[0], ['inzendtijd', 'title', 'punten'], 'title') except Exception as e: import Updates Updates.get_updates().error(e, 'get_opdrachten') return [], ['inzendtijd', 'title', 'punten'], 'title'
def get_hunts(): try: with requests.Session() as session: session.cookies.set('PHPSESSID', settings.Settings().phpsessid) r = session.get('http://www.jotihunt.net/groep/hunts.php') scraper = webscraper.TableHTMLParser() scraper.feed(r.text) with open('hunts.log', 'w') as f: f.write(r.text) scraper.fix_tables() # scraper.print_tables() return scraper.tables[0], ['hunttijd', 'meldtijd', 'code', 'status', 'toelichting', 'punten'], 'code' except Exception as e: import Updates Updates.get_updates().error(e, 'get_hunts') return [], ['hunttijd', 'meldtijd', 'code', 'status', 'toelichting', 'punten'], 'code'
def main(): updater = create_updater() t = StoppableThread() t.start() updater.start_polling() updater.idle() t.stop() updates = Updates.get_updates() updates.error(Exception("de bot gaat stoppen"), "de bot gaat stoppen") updates.save() t.join()
def bug_conversation(bot: telegram.Bot, update: telegram.Update, state): s = state.get_state() if update.message.chat_id > 0: keyboard = telegram.ReplyKeyboardMarkup(DEFAULT_KEYBOARD) else: keyboard = telegram.ReplyKeyboardHide() if s == 0: state['about'] = update.message.text message = 'stuur nu je bug, feature, tip of top.' state.next_state() elif s == 1: state['message'] = update.message.text state.done() message = 'Je input is opgeslagen en doorgestuurd naar Bram Micky ' \ 'en Mattijn (en evt. anderen).' else: message = ' dit is een fout in de bot graag aan mattijn laten weten,' \ 'bug_conversation' Updates.get_updates().error(Exception(message), 'bug_conversation') bot.sendMessage(update.message.chat_id, message, reply_markup=keyboard)
def sc_groep(bot: telegram.Bot, update: telegram.Update): """ :param bot: :param update: :return: """ chat_id = update.message.chat_id try: # TODO implement this # TODO add a keyboard bot.sendMessage(chat_id, 'Deze functie doet nog niks') Updates.get_updates().botan.track(update.message, 'groep') except MultipleConversationsError: bot.sendMessage(chat_id, "Er is al een commando actief je kunt dit " "commando niet starten.\n" " type /cancel om het vorige commando te " "stoppen te stoppen", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'incorrect_groep')
def get_hunts(): try: with requests.Session() as session: session.cookies.set('PHPSESSID', settings.Settings().phpsessid) r = session.get('http://www.jotihunt.net/groep/hunts.php') scraper = webscraper.TableHTMLParser() scraper.feed(r.text) with open('hunts.log', 'w') as f: f.write(r.text) scraper.fix_tables() # scraper.print_tables() return scraper.tables[0], [ 'hunttijd', 'meldtijd', 'code', 'status', 'toelichting', 'punten' ], 'code' except Exception as e: import Updates Updates.get_updates().error(e, 'get_hunts') return [], [ 'hunttijd', 'meldtijd', 'code', 'status', 'toelichting', 'punten' ], 'code'
def call(*args, **kwargs): try: func(*args, **kwargs) except Exception as e: print(str(e)) updates = Updates.get_updates() if func is not None: logging.error(str(e) + '\n' + func.__name__) updates.error(e, func.__name__) else: logging.error(str(e) + '\nIk weet het niet meer.') updates.error(e, '\nIk weet het niet meer.')
def call(*args, **kwargs): try: func(*args, **kwargs) except Exception as e: type_, value_, traceback_ = sys.exc_info() print(str(e)) updates = Updates.get_updates() if func is not None: logging.error(str(e) + '\n' + func.__name__) updates.error(e, func.__name__, (type_, value_, traceback_)) else: logging.error(str(e) + '\nIk weet het niet meer.') updates.error(e, '\nIk weet het niet meer.')
def cancel(bot, update): """ :param bot: :param update: :return: """ chat_id = update.message.chat_id user_id = update.message.from_user.id h = str(user_id) + str(chat_id) try: state = State.states[h] state.cancel() except KeyError: bot.sendMessage(chat_id, "Er is geen commando actief.", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'incorrect_cancel') else: bot.sendMessage(chat_id, "Het commando is gestopt.", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'cancel')
def run(self): logging.debug('5min uodate thread started') updater = Updates.get_updates() while not self.stopped(): try: updater.update() except Exception as e: logging.error('update error: ' + str(e)) updater.error(e, 'update_thread') i = 0 while i < 300 and not self.stopped(): di = 5 time.sleep(di) logging.debug('5min update thread stopped')
def set_url(bot: telegram.Bot, update: telegram.Update): chat_id = update.message.chat_id user_id = update.message.from_user.id try: state = State(bot, chat_id, user_id, set_url_conversation, set_url_done) state['command'] = update.message.text state['from'] = update.message.from_user.name bot.sendMessage(chat_id, "waar wil je de hunt voor instellen?\n " "hint/opdracht/nieuws", reply_to_message_id=update.message.message_id, reply_markup=telegram.ReplyKeyboardMarkup([['hint'], ['opdracht'], ['nieuws'], ['/cancel']])) Updates.get_updates().botan.track(update.message, 'set_url') except MultipleConversationsError: bot.sendMessage(chat_id, "Er is al een commando actief je kunt dit " "commando niet starten.\n" " type /cancel om het vorige commando te " "stoppen te stoppen", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'incorrect_set_url')
def sc_groep(bot, update): """ :param bot: :param update: :return: """ chat_id = update.message.chat_id try: # State(bot, chat_id, user_id, PhotoUpdates, change_opdracht_updates) # TODO implement this # bot.sendMessage(chat_id, "Moeten hint updates aan of uit staan?\ # naan/uit", # reply_to_message_id=update.message.message_id) # # TODO add a keyboard bot.sendMessage(chat_id, 'Deze functie doet nog niks') Updates.get_updates().botan.track(update.message, 'groep') except MultipleConversationsError: bot.sendMessage(chat_id, "Er is al een commando actief je kunt dit " "commando niet starten.\n" " type /cancel om het vorige commando te " "stoppen te stoppen", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'incorrect_groep')
def updates(bot: telegram.Bot, update: telegram.Update): chat_id = update.message.chat_id user_id = update.message.from_user.id try: State(bot, chat_id, user_id, updates_conversation, change_updates) message = "Waarvoor moeten updates aan of uit staan?\n " \ "deelgebied, nieuws, hints, opdracht, error" keyboard = [['deelgebied'], ['nieuws'], ['hints'], ['opdracht'], ['error'], ['/cancel']] kb = telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True, selective=True) bot.sendMessage(chat_id, message, reply_to_message_id=update.message.message_id, reply_markup=kb) Updates.get_updates().botan.track(update.message, 'update') except MultipleConversationsError: bot.sendMessage(chat_id, "Er is al een commando actief je kunt dit " "commando niet starten.\n" " type /cancel om het vorige commando te " "stoppen te stoppen", reply_to_message_id=update.message.message_id) Updates.get_updates().botan.track(update.message, 'incorrect_update')
def _updates_menu(self, update: Update, callback_query: str, rp_acc)->Tuple[str, List[InlineKeyboardButton]]: message = '' if callback_query == 'u_1': for u in Updates.get_updates().check_updates(update.effective_chat.id): message += u + '\n' return message, [] elif callback_query in ['hints', 'opdracht', 'nieuws', 'error', 'A', 'B', 'C', 'D', 'E', 'F', 'X']: self._get_next_buttons = self._updates_aan_uit_menu message = 'updates voor ' + str(self.path[-1]) + ' aan of uit zetten.' return message, [ InlineKeyboardButton('aan', callback_data='a'), InlineKeyboardButton('uit', callback_data='u') ] else: return 'error, waarschijnlijk heb je meerdere knoppen in het zelfde menu ingedrukt.\n' \ 'updates_menu, ' + str(callback_query), []
def run(self): logging.debug('5min uodate thread started') updater = Updates.get_updates() while not self.stopped(): try: updater.update() except Exception as e: logging.error('update error: ' + str(e)) updater.error(e, 'update_thread') start = time.time() end = time.time() while end-start < 60 and not self.stopped(): di = 5 time.sleep(di) end = time.time() logging.debug('1min update thread stopped')
def change_dg_updates(state): updates = Updates.get_updates() zet_aan = state['status'] in ['aan', 'Aan'] if state['deelgebied'] in ['a', 'A']: updates.set_updates(state.chat_id, Updates.ALPHA, zet_aan) elif state['deelgebied'] in ['b', 'B']: updates.set_updates(state.chat_id, Updates.BRAVO, zet_aan) elif state['deelgebied'] in ['c', 'C']: updates.set_updates(state.chat_id, Updates.CHARLIE, zet_aan) elif state['deelgebied'] in ['d', 'D']: updates.set_updates(state.chat_id, Updates.DELTA, zet_aan) elif state['deelgebied'] in ['e', 'E']: updates.set_updates(state.chat_id, Updates.ECHO, zet_aan) elif state['deelgebied'] in ['f', 'F']: updates.set_updates(state.chat_id, Updates.FOXTROT, zet_aan) elif state['deelgebied'] in ['x', 'X']: updates.set_updates(state.chat_id, Updates.XRAY, zet_aan)
def change_dg_updates(state): updates = Updates.get_updates() zet_aan = state['status'] in['aan', 'Aan'] if state['deelgebied'] in ['a', 'A']: updates.set_updates(state.chat_id, Updates.ALPHA, zet_aan) elif state['deelgebied'] in ['b', 'B']: updates.set_updates(state.chat_id, Updates.BRAVO, zet_aan) elif state['deelgebied'] in ['c', 'C']: updates.set_updates(state.chat_id, Updates.CHARLIE, zet_aan) elif state['deelgebied'] in ['d', 'D']: updates.set_updates(state.chat_id, Updates.DELTA, zet_aan) elif state['deelgebied'] in ['e', 'E']: updates.set_updates(state.chat_id, Updates.ECHO, zet_aan) elif state['deelgebied'] in ['f', 'F']: updates.set_updates(state.chat_id, Updates.FOXTROT, zet_aan) elif state['deelgebied'] in ['x', 'X']: updates.set_updates(state.chat_id, Updates.XRAY, zet_aan)
def conversation(bot: telegram.Bot, update: telegram.Update): """ :rtype: None :param bot: :param update: :return: """ updater = Updates.get_updates() if not updater.has_bot(): updater.add_bot(bot) chat_id = update.message.chat_id user_id = update.message.from_user.id h = str(user_id) + str(chat_id) try: state = State.states[h] except KeyError: return state.responer(bot, update, state) return
def send_reminder(self): self.last_warning = time.time() message = 'Reminder voor de opdracht: [{titel}]({url})\n' \ 'Hier kunnen we {maxpunten} punt{mv} mee ' \ 'verdienen. We kunnen hier nog {uren} uur en ' \ '{min} minuten over doen.' message = message.format(titel=self.titel, url=settings.Settings( ).base_opdracht_url+self.opdracht_id, maxpunten=self.maxpunten, mv=('en' if self.maxpunten != 1 else ''), uren=math.floor( self.remaining_time('uur')), min=math.floor( self.remaining_time( 'minuten') % 60)) u = Updates.get_updates() for chat_id in self.chat_ids: u.send_message(chat_id, message, parse_mode=ParseMode.MARKDOWN)
def set_url_done(state): s = settings.Settings() u = Updates.get_updates() m = 'de url voor {soort) is aangepast van {old} naar {new}' if state['soort'] == 'hint': m = m.format(soort='hints', old=str(s.base_hint_url), new=str(state['url'])) s.base_hint_url = state['url'] elif state['soort'] == 'opdracht': m = m.format(soort='opdrachten', old=str(s.base_opdracht_url), new=str(state['url'])) s.base_opdracht_url = state['url'] elif state['soort'] == 'nieuws': m = m.format(soort='nieuws', old=str(s.base_nieuws_url), new=str(state['url'])) s.base_nieuws_url = state['url'] u.error(Exception(m), 'set_url')
def conversation(bot, update): """ :rtype: None :param bot: :param update: :return: """ updater = Updates.get_updates() if not updater.has_bot(): updater.add_bot(bot) updater.update() chat_id = update.message.chat_id user_id = update.message.from_user.id h = str(user_id) + str(chat_id) try: state = State.states[h] except KeyError: return state.responer(bot, update, state) return
def _admin_menu(self, update: Update, callback_query: str, rp_acc)->Tuple[str, List[InlineKeyboardButton]]: if callback_query == 'a_1': return 'niet geimplenteerd', [] elif callback_query in ['uit', 'aan']: chats = IdsObserver() buttons = [] self._get_next_buttons = self._admin_menu_updates_group_1 for tid in chats.group_chats: buttons.append(InlineKeyboardButton(chats.group_chats[tid], callback_data=str(tid))) return 'voor welke chat wil je uupdates aan of uitzetten?', buttons elif callback_query == 'a_4': users = IdsObserver() users.send_users_buffer() return 'gebruikers zijn naar de site gestuurd', [] elif callback_query == 'a_5': users = IdsObserver() with users.users_lock: buttons = [] for user_id in users.user_buffer: buttons.append(InlineKeyboardButton(users.getName(user_id), callback_data=str(user_id))) self._get_next_buttons = self._admin_menu_remove_user return 'welke gebruiker moet worden verwijder uit de buffer?', buttons elif callback_query in ['reminder_uit', 'reminder_aan']: self._get_next_buttons = self._reminder_menu updates = Updates.get_updates() with updates._reminders_lock: buttons = [] for opdracht_id in updates.reminders: reminder: reminders.Reminder = updates.reminders[opdracht_id] message = reminder.titel if reminders.check_reminder(opdracht_id): message += ' ;staat nu aan' else: message += ' ;staat nu uit' buttons.append(InlineKeyboardButton(message, callback_data=opdracht_id)) return 'waar wil je reminders aan of uit voor zetten?', buttons else: return 'error, waarschijnlijk heb je meerdere knoppen in het zelfde menu ingedrukt.\n' \ 'admin_menu, ' + str(callback_query), []
def main(): if os.path.isfile(STARTUPFILE): with open(STARTUPFILE, 'rb') as file: start_up_command = pickle.load(file) if start_up_command['command'] == 'stop': with open(STARTUPFILE, 'wb') as file: pickle.dump({'command': 'start'}, file) return sett = Settings() api = RpApi.get_instance(sett.rp_username, sett.rp_pass) api.login() updater = create_updater() t = StoppableThread() t.start() updater.start_polling() updates = Updates.get_updates() updates.add_stopable_thread(t) updates.add_bot(updater.bot) updates.to_all('De bot is weer opgestart') updater.idle() updater.stop()
def _admin_menu_updates_group_2(self, update: Update, callback_query: str, rp_acc) -> Tuple[ str, List[InlineKeyboardButton]]: updates = Updates.get_updates() chat_id = self.path[-2] update_type = self.path[-1] zet_aan = self.path[-3] == 'aan' if zet_aan: message = 'updates voor ' + str(update_type) + ' zijn aangezet.' else: message = 'updates voor ' + str(update_type) + ' zijn uitgezet.' if update_type == 'A': updates.set_updates(chat_id, Updates.ALPHA, zet_aan) elif update_type == 'B': updates.set_updates(chat_id, Updates.BRAVO, zet_aan) elif update_type == 'C': updates.set_updates(chat_id, Updates.CHARLIE, zet_aan) elif update_type == 'D': updates.set_updates(chat_id, Updates.DELTA, zet_aan) elif update_type == 'E': updates.set_updates(chat_id, Updates.ECHO, zet_aan) elif update_type == 'F': updates.set_updates(chat_id, Updates.FOXTROT, zet_aan) elif update_type == 'X': updates.set_updates(chat_id, Updates.XRAY, zet_aan) elif update_type == 'hints': updates.set_updates(chat_id, Updates.HINTS, zet_aan) elif update_type == 'nieuws': updates.set_updates(chat_id, Updates.NIEUWS, zet_aan) elif update_type == 'opdracht': updates.set_updates(chat_id, Updates.OPDRACHTEN, zet_aan) elif update_type == 'error': updates.set_updates(chat_id, Updates.ERROR, zet_aan) else: message = 'error, waarschijnlijk heb je meerdere knoppen in het zelfde menu ingedrukt.\n' \ '_admin_menu_updates_group_2, ' + str(callback_query) return message, []
def start(bot, update): bot.sendMessage(update.message.chat_id, start_message) x = authenticate() x(lambda bot2, update2: print('authenticated:\n' + str(update.to_dict())))( bot, update) Updates.get_updates().botan.track(update.message, 'start')
def crash(bot, update): bot.sendMessage(update.message.chat_id, 'we gaan de bot proberen te ' 'crashen') Updates.get_updates().botan.track(update.message, '/crash') raise Exception("dit is een test")
def check_updates(bot, update): message = 'updates staan aan voor:\n' for u in Updates.get_updates().check_updates(update.message.chat_id): message += u + '\n' bot.sendMessage(update.message.chat_id, message) Updates.get_updates().botan.track(update.message, 'check_update')
def change_niews_updates(state): updates = Updates.get_updates() zet_aan = state['status'] in ['aan', 'Aan'] updates.set_updates(state.chat_id, Updates.NIEUWS, zet_aan)
def change_hints_updates(state): updates = Updates.get_updates() zet_aan = state['status'] in ['aan', 'Aan'] updates.set_updates(state.chat_id, Updates.HINTS, zet_aan)
def error_handler(bot, update, error): bot.sendMessage( update.message.chat_id, "er is in deze chat een error " "opgetreden:\n" + str(error)) Updates.get_updates().error(error, "Updater")
def change_error_updates(state): updates = Updates.get_updates() zet_aan = state['status'] in ['aan', 'Aan'] updates.set_updates(state.chat_id, Updates.ERROR, zet_aan)
def test(bot, update): bot.sendMessage(update.message.chat_id, 'de bot is online') url = Updates.get_updates().botan.shorten('google', update.message.from_user.id) bot.sendMessage(update.message.chat_id, url) Updates.get_updates().botan.track(update.message, 'test')
def change_opdracht_updates(state): updates = Updates.get_updates() zet_aan = state['status'] in ['aan', 'Aan'] updates.set_updates(state.chat_id, Updates.OPDRACHTEN, zet_aan)
def error_handler(bot: telegram.Bot, update: telegram.Update, error): bot.sendMessage(update.message.chat_id, "er is in deze chat een error " "opgetreden:\n" + str(error)) Updates.get_updates().error(error, "Updater")
def error(self, message): import Updates Updates.get_updates().error(Exception(str(message)), 'table parser')