def cancel_which_event(): markup = InlineKeyboardMarkup() markup.row_width = 2 for e in get_event(): markup.add(InlineKeyboardButton(e.get_inline_str(), callback_data="cb:cancel_which_event:" + e.event_id)) return markup
def send_reminder_for(from_user_id, _id=None): if _id[:2] == 'su': l = util_db.get_survey(survey_id=_id) elif _id[:2] == 'ev': l = util_db.get_event(event_id=_id) frichtle = util_db.get_frichtle() username = util_db.get_frichtle(user_id=from_user_id).get_inline_str() for f in frichtle: if f.user_id == from_user_id: continue if isinstance(l, Survey): needs = util_db.check_survey_response(user_id=f.user_id, survey_id=_id) == None markup = markups.selection.new_survey_alert() text = "{} bittet um Rückmeldung für folgende Umfrage:\n".format(username) + str(l) elif isinstance(l, Event): needs = util_db.check_attendance(user_id=f.user_id, event_id=_id) == 1 markup = markups.selection.new_event_alert() text = "{} bittet um Rückmeldung für folgenden Termin:\n".format(username) + str(l) else: needs = False if needs: bot.send_message(f.user_id, text=text, reply_markup=markup) logger.info("Send reminder for: {}".format(str(l).replace("\n", ";"))) return
def send_cancel_event_notice(who_cancel, event_id): frichtle = util_db.get_frichtle() ev = util_db.get_event(event_id=event_id) name = util_db.get_username(who_cancel).get_inline_str() text = "Benachrichtigung:\n{} hat folgenden Termin abgesagt:\n".format(name) + str(ev) for user_id in [f.user_id for f in frichtle]: if user_id == who_cancel: continue bot.send_message(chat_id=user_id, text=text)
def send_event_edit_notice(who_edit, event_id): frichtle = util_db.get_frichtle() ev = util_db.get_event(event_id=event_id) name = util_db.get_frichtle(user_id=who_edit).get_inline_str() text = "Benachrichtigung:\n{} hat folgenden Termin bearbeitet. Bitte überprüfe, ob deine Zu- oder Absage noch stimmt:\n"\ .format(name) + str(ev) for user_id in [f.user_id for f in frichtle]: if user_id == who_edit: continue bot.send_message(user_id, text=text)
def edit_property_reply(self, message): user_id = message.from_user.id event = database.get_event(event_id=self.store[user_id]['data']['event_id']) prop = self.store[user_id]['data']['property'] try: setattr(event, prop, message.text) self.store[user_id]['data']['new_value'] = message.text except ValueError: text = "Der neue Wert wurde nicht erkannt." markup = markups.menu.manage_events() bot.send_message(message.chat.id, text=text, reply_markup=markup) return self.__call__(user_message=message)
def ask_for_event(command, events=None, extra_buttons=None): if events is None: events = get_event() markup = InlineKeyboardMarkup(row_width=1) command = "cb:" + command + ":" if events is None: return for e in events: markup.add(InlineKeyboardButton(e.get_inline_str(), callback_data=command+e.event_id)) if extra_buttons: for b in extra_buttons: markup.add(b) markup.add(InlineKeyboardButton("Abbrechen", callback_data="abort")) return markup
def get_event_summary(event_id=None, event=None, detail=False, **kwargs): if event is not None: event_id = event.event_id else: event = util_db.get_event(event_id) summary = util_db.get_event_attendance(event_id) x1 = len(list(filter(lambda x: x['attendance'] == 3, summary))) x0 = len(list(filter(lambda x: x['attendance'] == 2, summary))) x_1 = len(list(filter(lambda x: x['attendance'] == 1, summary))) att = {'Beschreibung': str(event), 'Zusagen': x1, 'Absagen': x0, 'Ausstehend': x_1} for instr in meta.registers.values(): for i in instr: att[i] = get_attendance_from_instrument(summary, i) text = meta.event_summary_template.format(**att) if detail: for f in util_db.get_frichtle(): res = '\n' for res in summary: if res['user_id'] == f.user_id: r = res['attendance'] if r == 3: res = "Ja" elif r == 2: res = "Nein" else: res = "Keine Rückmeldung" break if res != '': text += "{}: {}\n".format(f.get_inline_str(), res) return text return meta.event_summary_template.format(**att)
def main(self, initial_message=None, user_message=None, call=None, inline_message=None, **kwargs): if inline_message: user_id = inline_message.chat.id else: user_id = (initial_message or user_message or call).from_user.id if call: inline_message = call.message if 'event_id' not in kwargs.keys(): text = "Wähle einen Termin aus, den du absagen möchtest:" markup = InlineKeyboardMarkup(row_width=1) buttons = list( InlineKeyboardButton(text=e.get_inline_str(), callback_data=self.command + json.dumps({'event_id': e.event_id})) for e in database.get_event()) markup.add(*tuple(buttons)) markup.add(InlineKeyboardButton("Abbrechen", callback_data="abort")) msg = bot.send_message(initial_message.chat.id, text=text, reply_markup=markup) self.store[user_id]['inline_message'] = msg return if 'sure' not in kwargs.keys(): event = database.get_event(event_id=kwargs.get('event_id')) text = "Bist du sicher, dass du folgenden Termin absagen möchtest?\n\n" + event.get_detail_str( ) markup = InlineKeyboardMarkup(row_width=2) markup.add( InlineKeyboardButton(text="Ja", callback_data=self.command + json.dumps({'sure': True})), InlineKeyboardButton(text="Nein", callback_data=self.command + json.dumps({'sure': False}))) markup.add( InlineKeyboardButton(text="Abbrechen", callback_data="abort")) bot.edit_message_text(inline_message=inline_message, text=text, reply_markup=markup) return if 'send_notice' not in kwargs.keys(): if not kwargs.get('sure'): text = "Schade, dann ist wohl etwas schiefgegangen... Bitte versuche es ggfs erneut." markup = None else: event = database.get_event(event_id=kwargs.get('event_id')) text = "Okay, der Termin wurde abgesagt.\n" \ "Möchtest du alle wissen lassen, dass der Termin abgesagt wurde?" markup = InlineKeyboardMarkup(row_width=2) markup.add( InlineKeyboardButton(text="Ja", callback_data=self.command + json.dumps({'send_notice': True})), InlineKeyboardButton(text="Nein", callback_data=self.command + json.dumps({'send_notice': False}))) markup.add( InlineKeyboardButton(text="Abbrechen", callback_data="abort")) bot.edit_message_text(inline_message=inline_message, text=text, reply_markup=markup) return if not kwargs.get('send_notice'): text = "Okay, es werden keine Nachrichten versandt." else: text = "Okay, es wurden Nachrichten an alle versandt." util.functions.send_cancel_event_notice( who_cancel=user_id, event_id=kwargs['event_id']) bot.edit_message_text(inline_message=inline_message, text=text) self.store[user_id] = dict() self.store[user_id]['data'] = dict() self.store[user_id]['inline_message'] = None return
def main(self, initial_message=None, user_message=None, call=None, inline_message=None, **kwargs): if inline_message: user_id = inline_message.chat.id else: user_id = (initial_message or user_message or call).from_user.id if call: inline_message = call.message buttons = [] markup = None events = util.database.get_event() if len(events) == 0: bot.reply_to(initial_message, text="Es gibt aktuell keine Termine.") return if kwargs.get('event_id') is None: text = "Wähle einen Termin aus:" markup = InlineKeyboardMarkup(row_width=1) buttons = list( InlineKeyboardButton(text=s.get_inline_str(), callback_data=self.command + json.dumps({'event_id': s.event_id})) for s in database.get_event()) markup.add(*tuple(buttons)) markup.add(InlineKeyboardButton("Beenden", callback_data="end")) if inline_message: msg = bot.edit_message_text(inline_message=inline_message, text=text, reply_markup=markup) else: msg = bot.send_message(initial_message.chat.id, text=text, reply_markup=markup) self.store[user_id]['inline_message'] = msg return event = util.database.get_event(event_id=kwargs['event_id']) text = util.functions.get_event_summary(**kwargs) if not kwargs.get('detail', False): # erinnerung if kwargs['event_id'] in [s.event_id for s in util.database.events_that_need_reminder()] and \ not self.store[user_id].get('sent_reminder', False): buttons.append( InlineKeyboardButton(text="Erinnern", callback_data=self.command + json.dumps({'remind': True}))) # detail if not kwargs.get('detail', False) and any( s['attendance'] >= 2 for s in util.database.get_event_attendance(event=event)): buttons.append( InlineKeyboardButton(text="Details", callback_data=self.command + json.dumps({'detail': True}))) # back button if kwargs.get('detail', False): dump = {'detail': False} else: dump = {'event_id': None} buttons.append( InlineKeyboardButton(text="Zurück", callback_data=self.command + json.dumps(dump))) markup = InlineKeyboardMarkup(row_width=2) markup.add(*tuple(buttons)) if kwargs.get('remind', False): util.functions.send_reminder_for(from_user_id=user_id, _id=kwargs['event_id']) self.store[user_id]['sent_reminder'] = True text = "Es wurden Erinnerungen versandt." dump = {'remind': False, 'detail': False} markup = InlineKeyboardMarkup(row_width=1) markup.add( InlineKeyboardButton(text="Zurück", callback_data=self.command + json.dumps(dump))) if markup is None and kwargs['detail']: markup = InlineKeyboardMarkup() markup.add( InlineKeyboardButton(text="Zurück", callback_data=self.command + json.dumps({'detail': False}))) if inline_message: msg = bot.edit_message_text(inline_message=inline_message, text=text, reply_markup=markup) else: msg = bot.send_message(initial_message.chat.id, text=text, reply_markup=markup) self.store[user_id]['inline_message'] = msg return
def main(self, initial_message=None, user_message=None, call=None, inline_message=None, **kwargs): if inline_message: user_id = inline_message.chat.id else: user_id = (initial_message or user_message or call).from_user.id if user_message: bot.delete_message(chat_id=user_message.chat.id, message_id=user_message.message_id) if call: inline_message = call.message if kwargs.get('event_id', None) is None: text = "Wähle einen Termin aus, den du bearbeiten möchtest:" markup = InlineKeyboardMarkup(row_width=1) buttons = list(InlineKeyboardButton(text=e.get_inline_str(), callback_data=self.command + json.dumps({'event_id': e.event_id})) for e in database.get_event()) markup.add(*tuple(buttons)) markup.add(InlineKeyboardButton("Abbrechen", callback_data="abort")) if inline_message: msg = bot.edit_message_text(inline_message=inline_message, text=text, reply_markup=markup) else: msg = bot.send_message(initial_message.chat.id, text=text, reply_markup=markup) self.store[user_id]['inline_message'] = msg return else: event = database.get_event(event_id=kwargs['event_id']) text = "Du bearbeitest folgenden Termin:\n" + event.get_detail_str() + "\n\n" if kwargs.get('property', None) is None: text += "Was möchtest du bearbeiten?" markup = InlineKeyboardMarkup(row_width=2) buttons = [] for i, v in enumerate(meta.event_col_names_display): buttons.append(InlineKeyboardButton(text=v, callback_data=self.command + json.dumps({'property': meta.event_col_names[i]}))) buttons.append(InlineKeyboardButton("Zurück", callback_data=self.command + json.dumps({'event_id': None}))) buttons.append(InlineKeyboardButton(text="Abbrechen", callback_data="abort")) markup.add(*tuple(buttons)) bot.edit_message_text(text=text, inline_message=inline_message, reply_markup=markup) return if kwargs['property'] == "dress": if kwargs.get('new_value') is None: text += "Okay, bitte wähle ein Outfit:" markup = InlineKeyboardMarkup(row_width=2) markup.add(*tuple([InlineKeyboardButton(text=s, callback_data=self.command + json.dumps( {'new_value': s} )) for s in meta.dresses] + [InlineKeyboardButton(text="Zurück", callback_data=self.command + json.dumps({'property': None, 'new_value': None}))])) bot.edit_message_text(text=text, inline_message=inline_message, reply_markup=markup) else: event = util.database.get_event(event_id=self.store[user_id]['data']['event_id']) event.dress = kwargs.pop('new_value') util.database.save_event(ev=event) self.store[user_id]['data'].pop('new_value') self.store[user_id]['data'].pop('property') self.__call__(call=call) return elif 'new_value' not in kwargs.keys(): prop_display = meta.event_col_names_display[meta.event_col_names.index(kwargs.get('property'))] text += "Okay, gib einen neuen Wert für '{}' ein:".format(prop_display) hint = meta.event_col_names_hints.get(kwargs['property']) if hint is not None: text += "\n{}".format(hint) msg = bot.edit_message_text(inline_message=inline_message, text=text) bot.register_next_step_handler(msg, self.edit_property_reply) return if 'sure' not in kwargs.keys(): # send sure reply markup event = database.get_event(event_id=kwargs.get('event_id')) setattr(event, kwargs.get('property'), kwargs.get('new_value')) text = "Der bearbeitete Termin ist jetzt:\n" + event.get_detail_str() + "\n\nSoll er gespeichert werden?" markup = InlineKeyboardMarkup(row_width=2) markup.add(InlineKeyboardButton(text="Ja", callback_data=self.command + json.dumps({'sure': True})), InlineKeyboardButton(text="Nein", callback_data=self.command + json.dumps({'sure': False}))) markup.add(InlineKeyboardButton(text="Abbrechen", callback_data="abort")) bot.edit_message_text(inline_message=inline_message, text=text, reply_markup=markup) return if 'send_notice' not in kwargs.keys(): if not kwargs.get('sure'): text = "Schade, dann ist wohl etwas schiefgegangen... Bitte versuche es ggfs erneut." markup = None else: event = database.get_event(event_id=kwargs.get('event_id')) setattr(event, kwargs.get('property'), kwargs.get('new_value')) database.save_event(event) text = "Okay, die Änderungen wurden gespeichert.\n" \ "Möchtest du alle wissen lassen, dass sich der Termin geändert hat?" markup = InlineKeyboardMarkup(row_width=2) markup.add(InlineKeyboardButton(text="Ja", callback_data=self.command + json.dumps({'send_notice': True})), InlineKeyboardButton(text="Nein", callback_data=self.command + json.dumps({'send_notice': False}))) markup.add(InlineKeyboardButton(text="Abbrechen", callback_data="abort")) bot.edit_message_text(inline_message=inline_message, text=text, reply_markup=markup) return if not kwargs.get('send_notice'): text = "Okay, es werden keine Nachrichten versandt." else: text = "Okay, es wurden Nachrichten an alle versandt." util.functions.send_event_edit_notice(who_edit=user_id, event_id=kwargs.get('event_id')) bot.edit_message_text(inline_message=inline_message, text=text) self.store[user_id] = dict() self.store[user_id]['data'] = dict() self.store[user_id]['inline_message'] = None return