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 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 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 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 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 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 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 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 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 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 __init__(self, map_file_name): """ Initializes the 2D array from the file. Also opens the log file. """ map_file = file(map_file_name) self._map_array = [list(line.strip()) for line in map_file.readlines()] map_file.close() self._validate() self.size = len(self._map_array) self.log_file = open(LOG_FILE, "w") self.updates = [Updates(), Updates()] # Now, read the map again for the sake of logging # TODO : find a better way of doing this. for i in range(self.size): for j in range(self.size): position = Position(i, j, self.size, self.size) self.set_symbol(position, self.get_symbol(position))
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_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 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 _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 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 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 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 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 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 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 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 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 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 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 _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 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 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 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 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 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 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(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 _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 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 main(repo, repourl, build, branch, buildtype, url=None, profile="minimal", readonlymode=""): # We need to iterate through the options in the map and find the right host based on # whether the repo name matches any of the options, as they may not be exactly identical if config.has_section(buildtype): for option in config.options(buildtype): line = config.get(buildtype, option) line = line.split(',') for entry in line: if option.strip() in repo: env.host = entry.strip() print "===> Host is %s" % env.host break # Didn't find any host in the map for this project. if env.host is None: raise ValueError( "===> You wanted to deploy a build but we couldn't find a host in the map file for repo %s so we're aborting." % repo) user = "******" # Set our host_string based on user@host env.host_string = '%s@%s' % (user, env.host) # Set SSH key if needed ssh_key = None if "*****@*****.**" in repourl: ssh_key = "/var/lib/jenkins/.ssh/id_rsa_github" # Set a URL if one wasn't already provided if url is None: url = "%s.%s.%s" % (repo, branch, env.host) # Run the tasks. # -------------- # If this is the first build, attempt to install the site for the first time. with settings(warn_only=True): if run('drush sa | grep \'^@\?%s_%s$\' > /dev/null' % (repo, branch)).failed: fresh_install = True else: fresh_install = False if fresh_install == True: print "===> Looks like the site %s doesn't exist. We'll try and install it..." % url try: common.Utils.clone_repo(repo, repourl, branch, build, None, ssh_key) InitialBuild.initial_build(repo, url, branch, build, profile) AdjustConfiguration.adjust_drushrc_php(repo, branch, build) common.Services.clear_php_cache() common.Services.clear_varnish_cache() common.Services.reload_webserver() InitialBuild.generate_drush_alias(repo, url, branch) Drupal.generate_drush_cron(repo, branch) except: e = sys.exc_info()[1] raise SystemError(e) else: print "===> Looks like the site %s exists already. We'll try and launch a new build..." % url # Grab some information about the current build previous_build = common.Utils.get_previous_build( repo, cleanbranch, build) previous_db = common.Utils.get_previous_db(repo, cleanbranch, build) Drupal.backup_db(repo, branch, build) common.Utils.clone_repo(repo, repourl, branch, build, None, ssh_key) Updates.merge_prod(repo, branch, build) AdjustConfiguration.adjust_settings_php(repo, branch, build, previous_build, buildtype) AdjustConfiguration.adjust_drushrc_php(repo, branch, build) AdjustConfiguration.adjust_files_symlink(repo, branch, build) Drupal.drush_status(repo, branch, build) Drupal.go_offline(repo, branch, build, readonlymode) # No need for this, drush_up does it later # drush_updatedb(repo, branch, build) # This will revert the database if it fails common.Utils.adjust_live_symlink( repo, branch, build) # This will revert the database if it fails Updates.drush_up(repo, branch) Drupal.drush_status( repo, branch, build, revert=True ) # This will revert the database if it fails (maybe hook_updates broke ability to bootstrap) Updates.add_push_updates(repo, branch, build) Drupal.go_online( repo, branch, build, previous_build, readonlymode ) # This will revert the database and switch the symlink back if it fails common.Services.clear_php_cache() common.Services.clear_varnish_cache() Drupal.generate_drush_cron(repo, branch) Tests.run_tests(repo, branch, build) #run_behat_tests(repo, branch, build) #commit_new_db(repo, repourl, url, build, branch) common.Utils.remove_old_builds(repo, branch) Updates.send_update_notification(repo, branch) script_dir = os.path.dirname(os.path.realpath(__file__)) if put(script_dir + '/../util/revert', '/home/jenkins', mode=0755).failed: print "####### BUILD COMPLETE. Could not copy the revert script to the application server, revert will need to be handled manually" else: print "####### BUILD COMPLETE. If you need to revert this build, run the following command: sudo /home/jenkins/revert -b %s -d %s -s /var/www/live.%s.%s -a %s_%s" % ( previous_build, previous_db, repo, branch, repo, branch)
dist = np.zeros((size, size)) p_ij = np.zeros((size, size)) for i in range(size): for j in range(size): dist[i, j] = np.absolute(x[i] - x[j]) p_ij[i, j] = 1 / (1 + dist[i, j]**beta) # CHANGE THIIIIIIIIIIIIS FROM GGP TO DOUBLEPL # alpha_doublepl = alpha*(c**(tau-sigma))/tau # t = (sigma*size/alpha_doublepl)**(1/sigma) # size è quella finale o originale? # u = TruncPois.tpoissrnd(t*w0) t = (sigma * size / alpha)**(1 / sigma) u = TruncPois.tpoissrnd(t * w) n = Updates.update_n(w, G, p_ij) # sui vecchi o nuovi w? # output = MCMC("w_gibbs", "exptiltBFRY", iter, sigma=sigma_true, tau=tau_true, alpha=alpha_true, u=u_true, # p_ij=p_ij, n=n_true) output = MCMC("w_gibbs", "GGP", iter, sigma_tau=0.08, tau=tau, sigma=sigma, alpha=alpha, u=u, n=n, p_ij=p_ij, c=c, w_init=w)
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 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 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 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 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_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_gan_rnn(): #Set up parameters #The time series for each element of the batch #sequenceLength x batch_size x 1 X_observed = T.tensor3() #Each sequence is either 1 or 0. Y = T.vector() sequenceLength = 1 batch_size = 100 #Discriminator on observed sample, discriminator on generated sample params_disc, p_x_observed = discriminator(X_observed, sequenceLength, batch_size, params=None) params_gen, x_gen = generator(sequenceLength, batch_size, params=None) params_disc, p_x_generated = discriminator(x_gen, sequenceLength, batch_size, params=params_disc) loss_generator = adversarial_loss_generator(p_x_generated=p_x_generated) loss_discriminator = adversarial_loss_discriminator( p_x_generated=p_x_generated, p_x_observed=p_x_observed) learning_rate = 0.0001 gen_updates = Updates.Updates(paramMap=params_gen, loss=loss_generator, learning_rate=learning_rate * 0.1) disc_updates = Updates.Updates(paramMap=params_disc, loss=loss_discriminator, learning_rate=learning_rate * 10.0) #Functions: #Train discriminator and generator #Train only discriminator #Generate values without training print "disc update keys", len(disc_updates.getUpdates().keys()) print "gen update keys", len(gen_updates.getUpdates().keys()) print "joined update keys", len( dict(gen_updates.getUpdates().items() + disc_updates.getUpdates().items())) generate_sample = theano.function(inputs=[], outputs=[x_gen]) trainDiscriminator = theano.function(inputs=[X_observed], updates=disc_updates.getUpdates()) trainAll = theano.function(inputs=[X_observed], outputs=[loss_generator, loss_discriminator], updates=dict(gen_updates.getUpdates().items() + disc_updates.getUpdates().items())) g = generate_sample() print g[0].shape sample_prob = theano.function(inputs=[X_observed], outputs=[p_x_observed]) sampled = sample_prob(g[0]) from DataTransformation.plotData import getData p50Lst = [] p90Lst = [] for epoch in range(0, 400): dataLst = getData() #seq_length x batch x 1 for ts in dataLst: if random.uniform(0, 1) < 0.0: trainDiscriminator(ts) else: loss_gen, loss_disc = trainAll(ts) print "loss gen", loss_gen print "loss disc", loss_disc #print sample_prob(ts) print "====================" print ts[0][0] print "sample_prob", sample_prob(ts)[0][0].tolist() #Plot dist print "pulling samples for evaluation" allSamples = [] for j in range(0, 16): samples = np.asarray(generate_sample()).flatten().tolist() allSamples += samples if random.uniform(0, 1) < 1.0: print sorted(allSamples) allTS = [] for ts in dataLst[0:12]: allTS += ts.flatten().tolist() binrange = np.arange(-2.0, 60.0, 1.0) p_data = [] for val in binrange: val_run = np.asarray([[[val]] * 100], dtype='float32') p_data += [sample_prob(val_run)[0][0][0]] plt.scatter(binrange, p_data) plt.hist(allTS, bins=binrange, normed=1, alpha=0.2) plt.hist(allSamples, bins=binrange, normed=1, alpha=0.2) plt.savefig(tmpDir + "hist_" + str(epoch) + ".png") plt.clf() #qqplot.qqplot(allSamples, allTS) print "mean samples", np.average(np.asarray(allSamples)) print "mean observed", np.average(np.asarray(allTS)) print "stdv samples", np.std(np.asarray(allSamples)) print "stdv observed", np.std(np.asarray(allTS)) print "p50 samples", np.percentile(np.asarray(allSamples), 50.0) print "p50 observed", np.percentile(np.asarray(allTS), 50.0) print "p90 samples", np.percentile(np.asarray(allSamples), 90.0) print "p90 observed", np.percentile(np.asarray(allTS), 90.0) p50Loss = abs( np.percentile(np.asarray(allSamples), 50.0) - np.percentile(np.asarray(allTS), 50.0)) p90Loss = abs( np.percentile(np.asarray(allSamples), 90.0) - np.percentile(np.asarray(allTS), 90.0)) p50Lst += [p50Loss] p90Lst += [p90Loss] plt.plot(p50Lst) plt.plot(p90Lst) plt.savefig(tmpDir + "progress_" + str(epoch) + ".png") plt.clf()
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')