Ejemplo n.º 1
0
 def on_message(cls, message: Message, **kwargs):
     if message.text.startswith("!coin"):
         yield Message(
             chat=message.chat,
             reply_to=message.id,
             text=random.choice(("heads", "tails")),
         )
Ejemplo n.º 2
0
 def on_message(cls, message: Message, **kwargs):
     if message.text.startswith("!dice"):
         dice_formula = message.text.replace("!dice", "").strip()
         if not dice_formula:
             dice_formula = cls.DEFAULT_FORMULA
         roll = int(dice.roll(dice_formula))
         yield Message(chat=message.chat, reply_to=message.id, text=roll)
Ejemplo n.º 3
0
 def on_message(cls, message, **kwargs):
     if "lo quito" in message.text.lower():
         yield Message(
             chat=message.chat,
             reply_to=message.id,
             text="Loquito tu.",
         )
Ejemplo n.º 4
0
 def on_message(cls, message, **kwargs):
     if message.text == "!ping":
         delta = datetime.now() - message.date
         delta_ms = delta.seconds * 1000 + delta.microseconds / 1000
         yield Message(
             chat=message.chat, reply_to=message.id, text=f"pong! ({delta_ms}ms)",
         )
Ejemplo n.º 5
0
    def parse_incoming_message(cls, request):
        data = request["json"]

        # Auth
        if data.get("token") != SLACK_TOKEN:
            raise cls.PlatformAuthError("Authentication error")

        # Confirms challenge request to configure webhook
        if "challenge" in data:
            raise cls.PlatformAuthResponse(
                data={"challenge": data["challenge"]})

        # Discard messages by webhooks and apps
        if "bot_id" in data["event"]:
            logger.debug("Discarding message", data=data)
            return

        logger.debug("Parsing message", platform=cls.ID, data=data)

        if data["event"]["type"] not in ("message", "message.groups"):
            return

        # Surprisingly, this *can* happen.
        if "text" not in data["event"]:
            return

        message = Message(
            id=data["event"].get("thread_ts", data["event"]["ts"]),
            author=data["event"].get("user"),
            from_bot="bot_id" in data["event"],
            date=datetime.fromtimestamp(int(float(data["event"]["event_ts"]))),
            text=data["event"]["text"],
            chat=data["event"]["channel"],
            channel=cls.parse_channel_from_message(data),
            raw=data,
        )

        logger.info(
            "New message",
            platform=message.channel.platform,
            channel=cls.parse_channel_name_from_raw(
                message.channel.channel_raw),
        )

        return message
Ejemplo n.º 6
0
    def parse_incoming_message(cls, request):
        request_data = request["json"]
        logger.debug("Parsing message", data=request_data, platform=cls.ID)

        return Message(
            id=str(uuid.uuid4()),
            date=datetime.now(),
            text=request_data["text"],
            from_bot=bool(request_data.get("from_bot", False)),
            author=request_data.get("author", "Debug author"),
            chat=request_data.get("chat", "Debug chat ID"),
            channel=Channel(
                platform=cls.ID,
                platform_channel_id=request_data.get("chat"),
                channel_raw={},
            ),
            raw={},
        )
Ejemplo n.º 7
0
    def parse_incoming_message(cls, request):
        token = request["path"].split("/")[-1]
        if token != TELEGRAM_TOKEN:
            raise cls.PlatformAuthError("Authentication error")

        logger.debug("Parsing message", data=request["json"], platform=cls.ID)

        if "text" in request["json"]["message"]:
            # Ignore all messages but text messages
            return Message(
                id=request["json"]["message"]["message_id"],
                date=datetime.fromtimestamp(request["json"]["message"]["date"]),
                text=str(request["json"]["message"]["text"]),
                from_bot=request["json"]["message"]["from"]["is_bot"],
                author=request["json"]["message"]["from"]["id"],
                chat=str(request["json"]["message"]["chat"]["id"]),
                channel=cls.parse_channel_from_message(
                    request["json"]["message"]["chat"]
                ),
                raw=request["json"],
            )