def process_email(self, message):
        user_id = message.from_user.id
        if message.text == '0':
            self.store[user_id]['data']['email'] = ''
            bot.delete_message(message=message)
            self.__call__(user_message=message)
        elif check_email(message.text):
            self.store[user_id]['data']['email'] = message.text
            bot.delete_message(message=message)
            self.__call__(user_message=message)
        else:
            text = "Die Email wurde nicht erkannt. Bitte versuche es erneut:"
            bot.delete_message(message=message)
            msg = bot.edit_message_text(inline_message=self.store[user_id]['inline_message'], text=text)
            bot.register_next_step_handler(msg, self.process_email)

        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 initial_message:
            bot.reply_to(initial_message, text=meta.new_event_guide)

        if kwargs.get('done', False):
            # send sure reply markup
            if kwargs.get('sure') is None:
                event = self.store[user_id]['event']
                text = "Der Termin ist lautet:\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})))
                markup.add(InlineKeyboardButton(text="Zurück", callback_data=self.command + json.dumps({'done': None,
                                                                                                        'property': None})))
                bot.edit_message_text(inline_message=inline_message, text=text, reply_markup=markup)
                return

            if kwargs.get('send_notice') is None:
                if not kwargs.get('sure'):
                    self.store[user_id]['property'] = None
                    self.__call__(call=call)
                    return
                else:
                    event = self.store[user_id]['event']
                    database.save_event(event)
                    text = "Okay, der Termin wurde erstellt.\n" \
                           "Möchtest du alle wissen lassen, sodass sie zu oder absagen können?"
                    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})))

                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_new_event_alert(by=util.database.get_frichtle(user_id=user_id),
                                                    event=self.store[user_id]['event'])

            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

        if kwargs.get('property') is None:
            e = self.store[user_id].get('event', Event())
            e.event_id = util.database.get_new_event_id()
            self.store[user_id]['event'] = e

            text = "Aktuell sieht der Termin folgendermaßen aus:\n" + e.get_detail_str() + "\n\nBitte wähle aus, um zu Daten einzugeben:"
            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(text="Fertig", callback_data=self.command + json.dumps({'done': True,
                                                                                                        'property': ''})))
            buttons.append(InlineKeyboardButton(text="Abbrechen", callback_data="abort"))
            markup.add(*tuple(buttons))

            if inline_message:
                msg = bot.edit_message_text(inline_message=inline_message, text=text, reply_markup=markup)
            elif call:
                msg = bot.edit_message_text(call=call, 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

        if kwargs['property'] == "dress":
            if kwargs.get('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(
                    {'value': s}
                )) for s in meta.dresses] +
                    [InlineKeyboardButton(text="Zurück", callback_data=self.command +
                                          json.dumps({'property': None, 'value': None}))]))
                bot.edit_message_text(text=text, inline_message=inline_message, reply_markup=markup)
            else:
                event = self.store[user_id]['event']
                event.dress = kwargs.pop('value')
                self.store[user_id]['data'].pop('value')
                self.store[user_id]['data'].pop('property')
                self.__call__(call=call)
            return

        else:
            prop_display = meta.event_col_names_display[meta.event_col_names.index(kwargs.get('property'))]
            text = "Okay, gib '{}' 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.property_reply)
            return
    def main(self, initial_message=None, user_message=None, call=None, inline_message=None, **kwargs):

        if initial_message:
            f = Frichtle.from_user(initial_message.from_user)
            user_id = f.user_id
            self.store[user_id]['new_frichtle'] = f

        else:
            if inline_message:
                user_id = inline_message.chat.id
            else:
                user_id = (initial_message or user_message or call).from_user.id
            f = self.store[user_id]['new_frichtle']

        text = "Hallo {},\n\n".format(f.first_name)

        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 inline_message is None:
            inline_message = self.store[user_id]['inline_message']

        if 'abort' in kwargs.keys():
            del self.store[user_id]
            bot.edit_message_text(inline_message=inline_message, text="Der Vorgang wurde abgebrochen und deine angegebenen Daten wurden gelöscht. Falls du möchtest, starte bitte erneut über den Einladungslink.\nLieben Gruß\nLucas")

        if 'done' in kwargs.keys() and kwargs.get('done'):
            util.database.add_frichtle(self.store[user_id]['new_frichtle'])
            bot.edit_message_text(inline_message=inline_message, text="Okay, du wurdest gespeichert und wirst jetzt zum Hauptmenü weitergeleitet.")
            bot.send_message(chat_id=user_id, text="Hauptmenü", reply_markup=markups.menu.main(user_id=user_id))
            return

        if initial_message:
            text += "Du hast folgenden Namen in Telegram hinterlegt:\n{} {}\nMöchtest du einen anderen Namen eingeben, mit dem dich andere sehen können? Ein Name, unter dem man dich erkennt wäre sehr hilfreich 😉".format(f.first_name, f.last_name)
            markup = InlineKeyboardMarkup(row_width=2)
            buttons = [InlineKeyboardButton(text="Ja, bitte", callback_data=self.command + json.dumps({'name_okay': False})),
                       InlineKeyboardButton(text="Nein, passt", callback_data=self.command + json.dumps({'name_okay': True}))]
            markup.add(*tuple(buttons))
            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

        if not kwargs.get('name_okay'):
            text += "Okay, bitte gib einen anderen Namen ein:"
            msg = bot.edit_message_text(inline_message=self.store[user_id]['inline_message'],
                                        text=text)
            bot.register_next_step_handler(msg, self.change_name_reply)
            return

        if kwargs.get('instr', None) is None:
            text += "Bitte wähle dein Instrument?"
            markup = InlineKeyboardMarkup(row_width=2)
            buttons = [InlineKeyboardButton(text=instr, callback_data=self.command + json.dumps({'instr': instr}))
                       for instr in meta.instruments]
            markup.add(*tuple(buttons))
            bot.edit_message_text(text=text, inline_message=inline_message, reply_markup=markup)
            return
        else:
            self.store[user_id]['new_frichtle'].instrument = kwargs.get('instr')

        if not kwargs.get('done', False):
            text += "Die folgenden Daten werden in einer verschlüsselten Datenbank abgelegt. Dieser Bot steht in keiner direkten Verbindung zum Verein und umgeht deshalb jegliche Richtlinien der DSGVO. Bei Fragen zur Datenspeicherung, kannst du jederzeit Lucas (@LucasBurger) anschreiben.\n\n"
            text += "Zusammenfassung:\n{}\n\nEinverstanden?".format(self.store[user_id]['new_frichtle'].get_detail_str())
            markup = InlineKeyboardMarkup()
            markup.add(InlineKeyboardButton(text="Instrument ändern", callback_data=self.command + json.dumps({'instr': None})))
            markup.add(InlineKeyboardButton(text="Name ändern",
                                            callback_data=self.command + json.dumps({'name_okay': False})))
            markup.add(InlineKeyboardButton(text="Einverstanden!",
                                            callback_data=self.command + json.dumps({'done': True})))
            bot.edit_message_text(text=text, inline_message=inline_message, reply_markup=markup)

        return
Beispiel #4
0
    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

        surveys = util.database.get_survey()

        if len(surveys) == 0:
            bot.reply_to(message=initial_message,
                         text="Es gibt aktuell keine Umfragen.")
            del self.store[user_id]
            return

        if call:
            inline_message = call.message

        if user_message:
            bot.delete_message(chat_id=user_message.chat.id,
                               message_id=user_message.message_id)

        if kwargs.get('survey_id', None) is None:
            text = "Welche Umfrage möchtest du anzeigen?"

            buttons = []
            for s in surveys:
                if util.database.check_survey_response(
                        user_id=user_id, survey_id=s.survey_id) is None:
                    replace = "Unbeantwortet: "
                else:
                    replace = ""
                buttons.append(
                    InlineKeyboardButton(
                        text=s.get_inline_str().replace("Umfrage: ", replace),
                        callback_data=self.command +
                        json.dumps({'survey_id': s.survey_id})))
            markup = InlineKeyboardMarkup(row_width=1)
            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

        survey = util.database.get_survey(survey_id=kwargs['survey_id'])

        if kwargs.get('edit', None) is None:
            text = survey.get_detail_str()
            res = survey.results[user_id]
            buttons = []
            if res is None:
                text += "\n\nDu hast noch nicht geantwortet."
                buttons.append(
                    InlineKeyboardButton(text="Jetzt antworten",
                                         callback_data=self.command +
                                         json.dumps({'edit': True})))
            else:
                text += "\n\nDu hast mit '{}' geantwortet".format(res)
                if survey.active:
                    buttons.append(
                        InlineKeyboardButton(text="Ändern",
                                             callback_data=self.command +
                                             json.dumps({'edit': True})))
            if not survey.active:
                text += "\nDie Umfrage ist geschlossen."
            buttons.append(
                InlineKeyboardButton(text="Zurück",
                                     callback_data=self.command +
                                     json.dumps({'survey_id': None})))

            markup = InlineKeyboardMarkup(row_width=2)
            markup.add(*tuple(buttons))
            bot.edit_message_text(inline_message=inline_message,
                                  text=text,
                                  reply_markup=markup)
            return

        if kwargs["edit"] and self.store[user_id]["data"].get(
                "response", None) is None:
            self.store[user_id]["data"]["response"] = [None] * len(
                survey.questions)

        res = kwargs.pop("res", None)
        if res is not None:
            self.store[user_id]["data"]["response"][res[0]] = res[1]

        i = 0
        while i < len(survey.questions):
            if self.store[user_id]["data"]["response"][i] is None:
                break
            i += 1

        if i < len(survey.questions):
            text = survey.get_detail_str(
                question=i) + "\n\nWie lautet deine Antwort?"
            if isinstance(survey.questions[i]["answers"], list):
                buttons = []
                for r in survey.questions[i]["answers"]:
                    buttons.append(
                        InlineKeyboardButton(text=r,
                                             callback_data=self.command +
                                             json.dumps({'res': [i, r]})))
                markup = InlineKeyboardMarkup(row_width=2)
                markup.add(
                    *tuple(buttons),
                    InlineKeyboardButton(text="Zurück",
                                         callback_data=self.command +
                                         json.dumps({'edit': None})))
            elif survey.questions[i]["answers"].lower() == 'count':
                page = kwargs.get('page', 1)
                markup = markups.inline.show_numbers(command=self.command,
                                                     return_name='res',
                                                     question=i,
                                                     page=page)
            else:
                # free reply
                text = survey.get_detail_str(
                ) + "\n\nOkay, gib deine Antwort ein:"
                msg = bot.edit_message_text(inline_message=inline_message,
                                            text=text)
                bot.register_next_step_handler(msg,
                                               self.survey_reply,
                                               question=i)
                return

            if i < len(survey.questions):
                bot.edit_message_text(inline_message=inline_message,
                                      text=text,
                                      reply_markup=markup)
                return

        util.database.set_response_to_survey(
            survey_id=kwargs['survey_id'],
            user_id=user_id,
            response=self.store[user_id]["data"]["response"])

        final_answer = " ".join(
            [str(r) for r in self.store[user_id]["data"]["response"]])

        text = f"\n\nDu hast mit '{final_answer}' geantwortet."

        text += "\n Wähle neue Umfrage aus:"

        buttons = []
        for s in surveys:
            if util.database.check_survey_response(
                    user_id=user_id, survey_id=s.survey_id) == '':
                replace = "Unbeantwortet: "
            else:
                replace = ""
            buttons.append(
                InlineKeyboardButton(text=s.get_inline_str().replace(
                    "Umfrage: ", replace),
                                     callback_data=self.command +
                                     json.dumps({'survey_id': s.survey_id})))
        markup = InlineKeyboardMarkup(row_width=1)
        markup.add(*tuple(buttons))
        markup.add(InlineKeyboardButton("Beenden", callback_data="end"))

        self.store[user_id]['data'].update({
            'survey_id': None,
            'edit': None,
            'res': None,
            "response": None
        })

        bot.edit_message_text(inline_message=inline_message,
                              text=text,
                              reply_markup=markup)
    def main(self,
             initial_message=None,
             call=None,
             inline_message=None,
             **kwargs):

        if inline_message:
            user_id = inline_message.chat.id
        else:
            user_id = (initial_message or call).from_user.id

        if call:
            inline_message = call.message

        if kwargs.get('typ', None) is None:
            text = "Bitte wähle aus:"
            markup = InlineKeyboardMarkup(row_width=1)
            markup.add(
                InlineKeyboardButton(text="Schritte",
                                     callback_data=self.command +
                                     json.dumps({'typ': "s"})))
            markup.add(
                InlineKeyboardButton(text="Märsche",
                                     callback_data=self.command +
                                     json.dumps({'typ': "m"})))
            markup.add(
                InlineKeyboardButton(text="Bühnenstücke",
                                     callback_data=self.command +
                                     json.dumps({'typ': "b"})))
            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

        typ = {
            "s": "Schritte",
            "m": "Maersche",
            "b": "Buehnenstuecke"
        }[kwargs['typ']]

        if kwargs.get('sheet_id', None) is None:
            noten = copy(meta.noten[typ])
            for i, n in enumerate(noten):
                v = n[0]
                v = v.replace("ae", "ä")
                v = v.replace("ue", "ü")
                v = v.replace("oe", "ö")
                v = v.replace("_", " ")
                noten[i] = (v, noten[i][1])

            markup = InlineKeyboardMarkup(row_width=1)
            buttons = list(
                InlineKeyboardButton(text=n[0],
                                     callback_data=self.command +
                                     json.dumps({'sheet_id': n[1]}))
                for n in noten)
            markup.add(*tuple(buttons))
            markup.add(
                InlineKeyboardButton("Zurück",
                                     callback_data=self.command +
                                     json.dumps({'typ': None})))
            bot.edit_message_text(text="Bitte wähle aus:",
                                  inline_message=inline_message,
                                  reply_markup=markup)

            return

        noten = meta.noten[typ]
        for l in noten:
            if l[1] == kwargs['sheet_id']:
                name = l[0]
                break

        noten_path = meta.noten_path.format(typ=typ, name=name)

        bot.delete_message(message=inline_message)

        bot.send_document(chat_id=inline_message.chat.id,
                          data=open(noten_path, "rb"),
                          caption="Bitteschön!")
    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