def send_cached_key_file(connection):
    """
    Send a message to a specific Threema ID with an already cached
    public key (stored in a file) of that recipient.
    """
    message = TextMessage(connection=connection,
                          id='ECHOECHO',
                          key_file='ECHOECHO.txt',
                          text='私はガラスを食べられます。それは私を傷つけません。')
    return (yield from message.send())
def send_cached_key_file(connection):
    """
    Send a message to a specific Threema ID with an already cached
    public key (stored in a file) of that recipient.
    """
    message = TextMessage(
        connection=connection,
        id='ECHOECHO',
        key_file='ECHOECHO.txt',
        text='私はガラスを食べられます。それは私を傷つけません。'
    )
    return (yield from message.send())
def send_cached_key(connection):
    """
    Send a message to a specific Threema ID with an already cached
    public key of that recipient.
    """
    message = TextMessage(
        connection=connection,
        id='ECHOECHO',
        key='public:4a6a1b34dcef15d43cb74de2fd36091be99fbbaf126d099d47d83d919712c72b',
        text='私はガラスを食べられます。それは私を傷つけません。'
    )
    return (yield from message.send())
def send_cached_key(connection):
    """
    Send a message to a specific Threema ID with an already cached
    public key of that recipient.
    """
    message = TextMessage(
        connection=connection,
        id='ECHOECHO',
        key=
        'public:4a6a1b34dcef15d43cb74de2fd36091be99fbbaf126d099d47d83d919712c72b',
        text='私はガラスを食べられます。それは私を傷つけません。')
    return (yield from message.send())
def send(connection):
    """
    Send a message to a specific Threema ID.

    Note that the public key will be automatically fetched from the
    Threema servers. It is strongly recommended that you cache
    public keys to avoid querying the API for each message.
    """
    message = TextMessage(connection=connection,
                          id='ECHOECHO',
                          text='私はガラスを食べられます。それは私を傷つけません。')
    return (yield from message.send())
def send(connection):
    """
    Send a message to a specific Threema ID.

    Note that the public key will be automatically fetched from the
    Threema servers. It is strongly recommended that you cache
    public keys to avoid querying the API for each message.
    """
    message = TextMessage(
        connection=connection,
        id='ECHOECHO',
        text='私はガラスを食べられます。それは私を傷つけません。'
    )
    return (yield from message.send())
    async def handle_threema_msg(self, message: Message):
        if type(message) == TextMessage:
            RECV_MESSAGE_COUNT.inc()
            message: TextMessage
            try:
                responses = self.bot.handle_input(message.text,
                                                  message.from_id)
                for response in responses:
                    await self.send_bot_response(message.from_id, response)
            except Exception as e:
                self.log.exception(
                    "An error happened while handling a Threema message",
                    exc_info=e)
                self.log.exception(
                    f"Message from {message.from_id}: {message.text}")
                self.log.exception("Exiting!")

                try:
                    response_msg = TextMessage(
                        self.connection,
                        text=adapt_text(self.bot.get_error_message().message,
                                        True),
                        to_id=message.from_id)
                    await response_msg.send()
                except Exception:
                    self.log.error(
                        f"Could not send message to {message.from_id}")

                try:
                    tb_list = traceback.format_exception(
                        None, e, e.__traceback__)
                    tb_string = ''.join(tb_list)

                    await self.sendMessageToDev(
                        f"An exception occurred: {tb_string}\n"
                        f"Message from {message.from_id}: {message.text}")
                except Exception:
                    self.log.error(f"Could not send message to developers")

                # Just exit on exception
                os.kill(os.getpid(), signal.SIGINT)
        elif type(message) == DeliveryReceipt:
            pass
        else:
            self.log.warning(
                f"Received unknown message type {type(message)}: {message}")
    async def send_bot_response(self, user: str, response: BotResponse):
        if response.images:
            for image in response.images:
                response_img = ImageMessage(self.connection,
                                            image_path=image,
                                            to_id=user)
                await response_img.send()
                SENT_IMAGES_COUNT.inc()

        if response.message:
            message_parts = split_message(adapt_text(str(response),
                                                     threema_format=True),
                                          max_bytes=3500)
            for m in message_parts:
                response_msg = TextMessage(self.connection, text=m, to_id=user)
                await response_msg.send()
                SENT_MESSAGE_COUNT.inc()