mirror of
https://github.com/langgenius/dify-plugin-sdks.git
synced 2026-07-22 18:35:29 -04:00
10da6e6cfa
- Applied chat_boost event pattern to all 20 Telegram event handlers - All event classes now inherit from both TelegramUpdateEvent and Event - Preserved custom _build_variables implementations where present (e.g., message_received.py) - Simple events now follow the minimal pattern with just update_key defined 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Mapping
|
|
from typing import Any
|
|
|
|
from dify_plugin.errors.trigger import EventIgnoreError
|
|
|
|
from dify_plugin.interfaces.trigger import Event
|
|
|
|
from ..base import TelegramUpdateEvent
|
|
|
|
|
|
class MessageReceivedEvent(TelegramUpdateEvent, Event):
|
|
"""Transform Telegram message updates into workflow variables."""
|
|
|
|
update_key = "message"
|
|
|
|
def _build_variables(
|
|
self,
|
|
payload: Mapping[str, Any],
|
|
update: Mapping[str, Any] | list[Any] | str | int | None,
|
|
parameters: Mapping[str, Any],
|
|
) -> dict[str, Any]:
|
|
message = dict(update or {})
|
|
chat = message.get("chat", {})
|
|
chat_type_filter = parameters.get("chat_type")
|
|
if chat_type_filter and chat.get("type") != chat_type_filter:
|
|
raise EventIgnoreError()
|
|
|
|
keywords = parameters.get("text_contains")
|
|
if keywords:
|
|
content = (message.get("text") or message.get("caption") or "").lower()
|
|
terms = [term.strip().lower() for term in keywords.split(",") if term.strip()]
|
|
if terms and not any(term in content for term in terms):
|
|
raise EventIgnoreError()
|
|
|
|
return {
|
|
"update_id": payload.get("update_id"),
|
|
"message": message,
|
|
}
|