コード例 #1
0
    def process_callback_query(self, user_id, chat_id, message_id, data):
        next_state = self
        command_array = data.split(" ")

        # only accept "/publish <draft/published>" callback queries
        if len(command_array) == 2 and command_array[0] == "/publish" and command_array[1] in [state.value for state in PostState]:

            publish_type = command_array[1]

            # check if previously selected post still exists
            post = self.bot.persistence.get_post(self.post_id)
            if post is not None:

                is_published = self.bot.publish(post.id, PostState(publish_type))

                # publishing successful
                if is_published:
                    self.bot.edit_message_text(chat_id, message_id
                                               , "<b>" + post.title + "</b> has been <b>published</b> as <b>"
                                               + ("draft" if publish_type == PostState.DRAFT.value else "final post") + "</b>."
                                               , parse_mode=ParseMode.HTML.value)

                    # if published as post -> go back to main menu
                    if publish_type == PostState.PUBLISHED.value:
                        from packages.states.navigation.idlestate import IdleState
                        next_state = IdleState(self.bot, user_id, chat_id=chat_id)
                    else:
                        next_state = SelectDraftUpdateState(self.bot, user_id, post.id, chat_id=chat_id)

                # publishing not successful
                else:
                    self.bot.edit_message_text(chat_id, self.message_id
                                               , "It seems publishing is not possible..."
                                               , parse_mode=ParseMode.HTML.value)
                    next_state = PublishDraftState(self.bot, user_id, post.id, chat_id=chat_id)

            # previously selected post no longer exists
            else:
                self.bot.send_message(chat_id
                                      , "It seems the draft you selected no longer exists..."
                                      , parse_mode=ParseMode.HTML.value)

                # show remaining drafts for updating
                user_drafts = self.bot.persistence.get_posts(user_id=user_id, status=PostState.DRAFT)
                if len(user_drafts) > 0:
                    from packages.states.draft.updatedraftstate import UpdateDraftState
                    next_state = UpdateDraftState(self.bot, user_id, chat_id=chat_id)
                # no remaining drafts -> automatically go back to main menu
                else:
                    from packages.states.navigation.idlestate import IdleState
                    next_state = IdleState(self.bot, user_id, chat_id=chat_id)

        else:
            next_state = super().process_callback_query(user_id, chat_id, message_id, data)

        return next_state
コード例 #2
0
    def process_callback_query(self, user_id, chat_id, message_id, data):
        next_state = self
        command_array = data.split(" ")

        if len(command_array) == 1:

            if command_array[0] == "/mainmenu":
                next_state = IdleState(self.bot,
                                       user_id,
                                       chat_id=chat_id,
                                       message_id=message_id)
            elif command_array[0] == "/createdraft":
                from packages.states.draft.createdraftstate import CreateDraftState
                next_state = CreateDraftState(self.bot,
                                              user_id,
                                              chat_id=chat_id,
                                              message_id=message_id)
            elif command_array[0] == "/updatedraft":
                from packages.states.draft.updatedraftstate import UpdateDraftState
                next_state = UpdateDraftState(self.bot,
                                              user_id,
                                              chat_id=chat_id,
                                              message_id=message_id)
            elif command_array[0] == "/deletedraft":
                from packages.states.draft.deletedraftstate import DeleteDraftState
                next_state = DeleteDraftState(self.bot,
                                              user_id,
                                              chat_id=chat_id,
                                              message_id=message_id)
            elif command_array[0] == "/updatepost":
                from packages.states.post.updatepoststate import UpdatePostState
                next_state = UpdatePostState(self.bot,
                                             user_id,
                                             chat_id=chat_id,
                                             message_id=message_id)
            elif command_array[0] == "/deletepost":
                from packages.states.post.deletepoststate import DeletePostState
                next_state = DeletePostState(self.bot,
                                             user_id,
                                             chat_id=chat_id,
                                             message_id=message_id)

        return next_state
コード例 #3
0
    def process_message(self, user_id, chat_id, text, entities):
        next_state = self
        text = text.strip(' \t\n\r')

        # let super() handle any bot commands
        if text.startswith("/") or "bot_command" in [
                entity["type"] for entity in entities
        ]:
            next_state = super().process_message(user_id, chat_id, text,
                                                 entities)

        # accept simple text as tag list
        else:
            # remove inline keyboard from latest bot message (by leaving out reply_options parameter)
            self.build_state_message(chat_id,
                                     self.welcome_message,
                                     message_id=self.message_id)

            # check if previously selected post still exists
            post = self.bot.persistence.get_post(self.post_id)
            if post is not None:

                new_tags = []
                for new_tag_name in [
                        x.strip(' \t\n\r')
                        for x in re.split("[,\t\n\r]", text)
                ]:

                    # ignore "empty" tags
                    if len(new_tag_name) == 0:
                        continue

                    tag = self.bot.persistence.add_post_tag(
                        post.id, new_tag_name)
                    if tag is not None:
                        new_tags.append(tag.name)

                if len(new_tags) > 0:
                    self.bot.send_message(chat_id,
                                          "Tag(s) <b>" + ", ".join(new_tags) +
                                          "</b> have been added to draft <b>" +
                                          post.title + "</b>.",
                                          parse_mode=ParseMode.HTML.value)
                else:
                    self.bot.send_message(
                        chat_id,
                        "<b>No tags(s) added</b>. All specified tag(s) were already assigned to draft <b>"
                        + post.title + "</b>.",
                        parse_mode=ParseMode.HTML.value)

                next_state = AddTagState(self.bot,
                                         user_id,
                                         self.post_id,
                                         chat_id=chat_id)

            # previously selected post no longer exists
            else:
                self.bot.send_message(
                    chat_id,
                    "It seems the draft you selected no longer exists...",
                    parse_mode=ParseMode.HTML.value)

                # show remaining drafts for updating
                user_drafts = self.bot.persistence.get_posts(
                    user_id=user_id, status=PostState.DRAFT)
                if len(user_drafts) > 0:
                    from packages.states.draft.updatedraftstate import UpdateDraftState
                    next_state = UpdateDraftState(self.bot,
                                                  user_id,
                                                  chat_id=chat_id)
                # no remaining drafts -> automatically go back to main menu
                else:
                    from packages.states.navigation.idlestate import IdleState
                    next_state = IdleState(self.bot, user_id, chat_id=chat_id)

        return next_state
コード例 #4
0
    def process_message(self, user_id, chat_id, text, entities):
        next_state = self
        text = text.strip(' \t\n\r')

        # let super() handle any bot commands
        if text.startswith("/") or "bot_command" in [
                entity["type"] for entity in entities
        ]:
            next_state = super().process_message(user_id, chat_id, text,
                                                 entities)

        # accept simple text as new post title
        else:
            # remove inline keyboard from latest bot message (by leaving out reply_options parameter)
            self.build_state_message(chat_id,
                                     self.welcome_message,
                                     message_id=self.message_id)

            # check if previously selected post still exists
            post = self.bot.persistence.get_post(self.post_id)
            if post is not None:
                new_title = text

                updated_post = self.bot.persistence.update_post(
                    post.id, post.user.id, new_title, post.status,
                    post.gallery.title, post.content,
                    None if post.title_image is None else post.title_image.id,
                    post.tmsp_publish, None
                    if post.original_post is None else post.original_post.id)

                # post update successful
                if updated_post is not None:
                    self.bot.send_message(
                        chat_id,
                        "Draft title has been updated to <b>" +
                        updated_post.title + "</b>.",
                        parse_mode=ParseMode.HTML.value)
                # post update not successful
                else:
                    self.bot.send_message(chat_id,
                                          "Draft title could not be updated.",
                                          parse_mode=ParseMode.HTML.value)

                next_state = SelectDraftUpdateState(self.bot,
                                                    user_id,
                                                    self.post_id,
                                                    chat_id=chat_id)

            # previously selected post no longer exists
            else:
                self.bot.send_message(
                    chat_id,
                    "It seems the draft you selected no longer exists...",
                    parse_mode=ParseMode.HTML.value)

                # show remaining drafts for updating
                user_drafts = self.bot.persistence.get_posts(
                    user_id=user_id, status=PostState.DRAFT)
                if len(user_drafts) > 0:
                    from packages.states.draft.updatedraftstate import UpdateDraftState
                    next_state = UpdateDraftState(self.bot,
                                                  user_id,
                                                  chat_id=chat_id)
                # no remaining drafts -> automatically go back to main menu
                else:
                    from packages.states.navigation.idlestate import IdleState
                    next_state = IdleState(self.bot, user_id, chat_id=chat_id)

        return next_state
コード例 #5
0
    def process_callback_query(self, user_id, chat_id, message_id, data):
        next_state = self
        command_array = data.split(" ")

        # only accept "/previewcontent" callback queries
        if len(command_array) == 1 and command_array[0] == "/previewcontent":

            # check if previously selected post still exists
            post = self.bot.persistence.get_post(self.post_id)
            if post is not None:

                # replace edit instructions with current draft content
                self.bot.edit_message_text(chat_id, self.message_id, post.content)

                next_state = EditDraftContentState(self.bot, user_id, post.id, chat_id=chat_id)

            # previously selected post no longer exists
            else:
                self.bot.send_message(chat_id
                                      , "It seems the draft you selected no longer exists..."
                                      , parse_mode=ParseMode.HTML.value)

                # show remaining drafts for updating
                user_drafts = self.bot.persistence.get_posts(user_id=user_id, status=PostState.DRAFT)
                if len(user_drafts) > 0:
                    from packages.states.draft.updatedraftstate import UpdateDraftState
                    next_state = UpdateDraftState(self.bot, user_id, chat_id=chat_id)
                # no remaining drafts -> automatically go back to main menu
                else:
                    from packages.states.navigation.idlestate import IdleState
                    next_state = IdleState(self.bot, user_id, chat_id=chat_id)

        # only accept "/formatting" callback queries
        elif len(command_array) == 1 and command_array[0] == "/formatting":

            # check if previously selected post still exists
            post = self.bot.persistence.get_post(self.post_id)
            if post is not None:

                message = "Following <b>Markdown</b> options are supported:\r\n\r\n" \
                    + "Heading 1-6: #..# text\r\n" \
                    + "Bold text: <b>**text**</b>\r\n" \
                    + "Italic text: <i>*text*</i>\r\n" \
                    + "Web link: [text](http://www.example.com/)\r\n" \
                    + "Inline code: <code>`some code`</code>\r\n" \
                    + "Pre-formatted code block <pre>```some code```</pre>"

                # replace edit instructions with Markdown formatting options
                self.bot.edit_message_text(chat_id, self.message_id, message, parse_mode=ParseMode.HTML.value)

                next_state = EditDraftContentState(self.bot, user_id, post.id, chat_id=chat_id)

            # previously selected post no longer exists
            else:
                self.bot.send_message(chat_id
                                      , "It seems the draft you selected no longer exists..."
                                      , parse_mode=ParseMode.HTML.value)

                # show remaining drafts for updating
                user_drafts = self.bot.persistence.get_posts(user_id=user_id, status=PostState.DRAFT)
                if len(user_drafts) > 0:
                    from packages.states.draft.updatedraftstate import UpdateDraftState
                    next_state = UpdateDraftState(self.bot, user_id, chat_id=chat_id)
                # no remaining drafts -> automatically go back to main menu
                else:
                    from packages.states.navigation.idlestate import IdleState
                    next_state = IdleState(self.bot, user_id, chat_id=chat_id)

        else:
            next_state = super().process_callback_query(user_id, chat_id, message_id, data)

        return next_state
コード例 #6
0
    def process_photo_message(self,
                              user_id,
                              chat_id,
                              file_id,
                              thumb_id=None,
                              caption=None):
        next_state = self

        # remove inline keyboard from latest bot message (by leaving out reply_options parameter)
        self.build_state_message(chat_id,
                                 self.welcome_message,
                                 message_id=self.message_id)

        # check if previously selected post still exists
        post = self.bot.persistence.get_post(self.post_id)
        if post is not None:

            image = None
            # TODO move this code to bot? -> file = self.context.download_file(file_id)
            telegram_file = self.bot.get_file(file_id)
            if "result" in telegram_file and "file_path" in telegram_file[
                    "result"]:
                file_url = telegram_file["result"]["file_path"]
                file = self.bot.download_file(file_url)

                if file is not None:
                    file_name = str(self.message_id) + "." + file_url.rsplit(
                        ".", 1)[1]
                    image = self.bot.persistence.add_post_image(
                        post.id,
                        file_id,
                        file_name,
                        file,
                        thumb_id=thumb_id,
                        caption=caption)

            if image is not None:
                self.bot.send_message(
                    chat_id,
                    "Image <b>" + image.name +
                    "</b> has been <b>added</b> to draft <b>" + post.title +
                    "</b>.",
                    parse_mode=ParseMode.HTML.value)
            else:
                self.bot.send_message(chat_id,
                                      "<b>Image not added</b> to draft <b>" +
                                      post.title + "</b>.",
                                      parse_mode=ParseMode.HTML.value)

            next_state = AddImageState(self.bot,
                                       user_id,
                                       post.id,
                                       chat_id=chat_id)

        # previously selected post no longer exists
        else:
            self.bot.send_message(
                chat_id,
                "It seems the draft you selected no longer exists...",
                parse_mode=ParseMode.HTML.value)

            # show remaining drafts for updating
            user_drafts = self.bot.get_user_posts(user_id=user_id,
                                                  status=PostState.DRAFT)
            if len(user_drafts) > 0:
                from packages.states.draft.updatedraftstate import UpdateDraftState
                next_state = UpdateDraftState(self.bot,
                                              user_id,
                                              chat_id=chat_id)
            # no remaining drafts -> automatically go back to main menu
            else:
                from packages.states.navigation.idlestate import IdleState
                next_state = IdleState(self.bot, user_id, chat_id=chat_id)

        return next_state