Beispiel #1
0
    def debug_button_template(self):
        try:
            # Actions
            postback_action = PostbackAction(label="postback lbl",
                                             data="postback data",
                                             display_text="postback text")
            message_action = MessageAction(label="message lbl",
                                           text="message text")
            dt_picker_action = DatetimePickerAction(label="dt lbl",
                                                    data="dt_picker data",
                                                    mode="datetime")
            camera_roll_action = CameraRollAction(label="croll lbl")

            # Button template
            b_template = ButtonsTemplate(text="Emergency Meeting",
                                         title="Start emergency meeting",
                                         actions=[
                                             postback_action, message_action,
                                             dt_picker_action,
                                             camera_roll_action
                                         ])

            # Template Mesage
            template_message = TemplateSendMessage(
                alt_text="a button template", template=b_template)
            global_settings.line_bot_api.reply_message(self.event.reply_token,
                                                       template_message)
        except Exception as exc:
            logger.exception(exc)
            return False
        return True
Beispiel #2
0
    def simple_chat_bot_text_reply(self) -> bool:
        """
        Reply if received text message is EXACTLY a keyword in our keywords

        Return True if we reply with a text message
        Return False otherwise
        """
        try:
            text_message = ""
            clean_message = self.message.lower().strip()
            for reply_text, keywords in constants.text_dict.items():
                for keyword in keywords:
                    if keyword.lower().strip() == clean_message:
                        text_message = reply_text
                        break
                else:
                    continue
                break

            if text_message == "":
                return False
            else:
                global_settings.line_bot_api.reply_message(
                    self.event.reply_token, TextSendMessage(text=text_message))
        except Exception as exc:
            logger.exception(exc)
            return False
        return True
Beispiel #3
0
    def emergency_meeting_template_bot(self):
        try:
            # Actions
            postback_action = PostbackAction(
                label="Start Now",
                data="send emergency meeting image",
                display_text=None)

            dt_picker_action = DatetimePickerAction(label="WIP",
                                                    data="dt_picker data",
                                                    mode="datetime")

            # Button template
            b_template = ButtonsTemplate(
                title="Emergency meeting",
                text="Emergency Meeting",
                thumbnail_image_url=constants.emergency_button_image_url,
                image_aspect_ratio="square",
                actions=[postback_action, dt_picker_action])

            # Template Mesage
            template_message = TemplateSendMessage(
                alt_text="Emergency Meeting", template=b_template)
            global_settings.line_bot_api.reply_message(self.event.reply_token,
                                                       template_message)
        except Exception as exc:
            logger.exception(exc)
            return False
        return True
Beispiel #4
0
    def crypto_coin_data_reply(self):
        """
        Reply current BTC, ETH, ADA, BNB

        Return True if successful
        Return False otherwise
        """
        t = PrettyTable(["Coin", "Latest Price"])
        help_message = str(t)
        try:
            raw_data_response = global_settings.cmc.cryptocurrency_quotes_latest(
                symbol="btc,eth,ada,bnb")
            data = raw_data_response.data
            btc_price = data["BTC"]["quote"]["USD"]["price"]
            eth_price = data["ETH"]["quote"]["USD"]["price"]
            bnb_price = data["BNB"]["quote"]["USD"]["price"]
            ada_price = data["ADA"]["quote"]["USD"]["price"]

            t.add_row(["BTC", btc_price])
            t.add_row(["ETH", eth_price])
            t.add_row(["BNB", bnb_price])
            t.add_row(["ADA", ada_price])

            help_message = str(t)

            global_settings.line_bot_api.reply_message(
                self.event.reply_token, TextSendMessage(text=help_message))
        except Exception as e:
            logger.exception(e)
            return False
        return True
Beispiel #5
0
    def simple_chat_bot_image_reply(self) -> bool:
        """
        Return True if we reply with an image
        Return False otherwise
        """
        try:
            url = ""
            lower_message = self.message.lower()
            for img_url, keywords in constants.image_dict.items():
                for keyword in keywords:
                    if keyword in lower_message:
                        url = img_url
                        break
                else:
                    continue
                break

            if url == "":
                return False
            else:
                image_message = ImageSendMessage(original_content_url=url,
                                                 preview_image_url=url)
                global_settings.line_bot_api.reply_message(
                    self.event.reply_token, image_message)
        except Exception as exc:
            logger.exception(exc)
            return False
        return True
def load_text_dict():
    try:
        with open("./chat/text_chat.json") as f:
            return json.load(f)
    except Exception as exc:
        logger.exception(exc)
        return {}
Beispiel #7
0
 def show_help(self):
     help_message = "Commands:\nboybot help\nboybot resend"
     try:
         global_settings.line_bot_api.reply_message(
             self.event.reply_token, TextSendMessage(text=help_message))
     except Exception as e:
         logger.exception(e)
         return False
     return True
Beispiel #8
0
 def start_nonadmin_message_process(self):
     try:
         if self.message.startswith("boybot resend"):
             self.resend()
         elif self.message == "boybot help":
             self.show_help()
         elif self.message == "boybot meeting":
             self.emergency_meeting_template_bot()
         elif self.message == "boybot coin":
             self.crypto_coin_data_reply()
     except Exception as e:
         logger.exception(e)
         return False
     return True
Beispiel #9
0
 def leave(self) -> bool:
     try:
         if self.event_source_type == "room":
             global_settings.line_bot_api.leave_room(
                 self.event.source.room_id)
         elif self.event_source_type == "group":
             global_settings.line_bot_api.leave_group(
                 self.event.source.group_id)
         else:
             print("Nothing to leave")
     except LineBotApiError as e:
         logger.exception(e)
         return False
     return True
Beispiel #10
0
 def save_to_db(self):
     post = {
         "message_id": self.event.message.id,
         "message_text": self.event.message.text,
         "source_user_id": self.event.source.user_id,
         "message_timestamp": self.event.timestamp
     }
     try:
         collection = self.get_collection()
         if collection is None:
             return False
         collection.insert_one(post)
     except Exception as exc:
         logger.exception(exc)
         return False
     return True
Beispiel #11
0
    def start_unsend_process(self):
        if not global_settings.instant_resend:
            return True
        try:
            unsend_message_id = self.event.unsend.message_id
            collection = self.get_collection()
            results_cursor = collection.find({"message_id": unsend_message_id})
            num_found = collection.count_documents(
                {"message_id": unsend_message_id})
            if num_found == 1:
                unsend_message = results_cursor[0]["message_text"]
                unsend_source_user_id = results_cursor[0]["source_user_id"]
                unsend_message_timestamp = results_cursor[0][
                    "message_timestamp"]
                # Add 8 hours in milliseconds (+ 28,800,000)
                dt = datetime.datetime.fromtimestamp(
                    (unsend_message_timestamp + 28800000) / 1000.0).isoformat(
                        " ", "seconds")

                if unsend_source_user_id == os.getenv(
                        "SUPERUSER_ID") and global_settings.superuser_mode:
                    return True

                if self.event_source_type == "room":
                    profile = global_settings.line_bot_api.get_room_member_profile(
                        self.get_room_or_group_id(), unsend_source_user_id)
                    display_name = profile.display_name
                elif self.event_source_type == "group":
                    profile = global_settings.line_bot_api.get_group_member_profile(
                        self.get_room_or_group_id(), unsend_source_user_id)
                    display_name = profile.display_name
                else:
                    display_name = unsend_source_user_id

                message = f"{display_name} {dt}: {unsend_message}"
                global_settings.line_bot_api.push_message(
                    self.get_room_or_group_id(), TextSendMessage(text=message))
            else:
                print(
                    "Ambiguous unsend, probably unsent message is a bbcon command or non text"
                )
        except Exception as e:
            logger.exception(e)
            return False
        return True
Beispiel #12
0
    def resend(self) -> bool:
        try:
            collection = self.get_collection()
            if collection is None:
                return False

            message_args = self.message.split()
            if len(message_args) == 3:
                user_arg = int(message_args[2])

            num_to_resend = min(collection.count_documents({}), user_arg)
            resend_message = ""
            room_or_group_id = self.get_room_or_group_id()

            for document in collection.find().sort(
                    "_id", pymongo.DESCENDING)[:num_to_resend]:
                # Add 8 hours in milliseconds (+ 28,800,000)
                dt = datetime.datetime.fromtimestamp(
                    (document['message_timestamp'] + 28800000) /
                    1000.0).isoformat(" ", "seconds")
                if document['source_user_id'] == os.getenv(
                        "SUPERUSER_ID") and global_settings.superuser_mode:
                    continue

                if self.event_source_type == "room":
                    profile = global_settings.line_bot_api.get_room_member_profile(
                        room_or_group_id, document['source_user_id'])
                    display_name = profile.display_name
                elif self.event_source_type == "group":
                    profile = global_settings.line_bot_api.get_group_member_profile(
                        room_or_group_id, document['source_user_id'])
                    display_name = profile.display_name
                else:
                    display_name = document['source_user_id']

                resend_message += f"{display_name} {dt}: {document['message_text']}"
                resend_message += "\n----------\n"

            global_settings.line_bot_api.reply_message(
                self.event.reply_token, TextSendMessage(text=resend_message))
        except Exception as exc:
            logger.exception(exc)
            return False
        return True
Beispiel #13
0
    def process_postback(self):
        try:
            postback_data = self.event.postback.data
            url = ""

            if postback_data == "send emergency meeting image":
                url = constants.emergency_meeting_image_url

            if url == "":
                return False
            else:
                image_message = ImageSendMessage(original_content_url=url,
                                                 preview_image_url=url)
                global_settings.line_bot_api.reply_message(
                    self.event.reply_token, image_message)
        except Exception as exc:
            logger.exception(exc)
            return False
        return True
Beispiel #14
0
 def start_message_process(self):
     try:
         if self.message == "bbcon leave":
             self.leave()
         elif self.message == "bbcon instant":
             global_settings.instant_resend = True
         elif self.message == "bbcon manual":
             global_settings.instant_resend = False
         elif self.message.startswith("bbcon resend"):
             self.resend()
         elif self.message == "bbcon enable superuser":
             global_settings.superuser_mode = True
         elif self.message == "bbcon disable superuser":
             global_settings.superuser_mode = False
         elif self.message == "bbcon button debug":
             self.debug_button_template()
     except Exception as e:
         logger.exception(e)
         return False
     return True
Beispiel #15
0
    def get_collection(self):
        try:
            # Find corresponding collection name from room id or group id or default
            collection_name = ""
            if self.event_source_type == "room":
                collection_name = self.event.source.room_id
            elif self.event_source_type == "group":
                collection_name = self.event.source.group_id
            else:
                collection_name = os.getenv("MONGODB_COLLECTION")

            # Return collection from collection name, create new capped collection if does not exist
            if collection_name not in global_settings.db.list_collection_names(
            ):
                global_settings.db.create_collection(collection_name,
                                                     capped=True,
                                                     size=5000000,
                                                     max=500)
            return global_settings.db[collection_name]
        except Exception as exc:
            logger.exception(exc)
            return None