ForcePilot/backend/package/yuxi/channel/extensions/msteams/proactive.py

81 lines
2.5 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
from .conversation_store import ConversationStore
from .outbound import send_media, send_text
from .sdk import BotFrameworkAdapter
from .types import BotFrameworkActivity, StoredConversationReference
logger = logging.getLogger(__name__)
async def build_conversation_reference(
activity: BotFrameworkActivity,
bot_app_id: str,
) -> StoredConversationReference:
return StoredConversationReference(
activity_id=activity.id,
user={
"id": activity.from_id,
"name": activity.from_name,
"aadObjectId": activity.from_.aad_object_id if activity.from_ else None,
},
bot={
"id": f"28:{bot_app_id}",
"name": "ForcePilot",
},
conversation={
"id": activity.conversation_id,
"conversationType": activity.conversation_type,
"tenantId": activity.tenant_id,
"isGroup": activity.is_group,
},
service_url=activity.service_url,
channel_id=activity.channel_id,
tenant_id=activity.tenant_id,
aad_object_id=activity.from_.aad_object_id if activity.from_ else None,
team_id=activity.channel_data.team_id if activity.channel_data else None,
channel_ref_id=activity.channel_data.channel_id if activity.channel_data else None,
thread_id=activity.reply_to_id if activity.is_channel_thread else None,
locale=activity.locale,
)
async def save_conversation_reference(
store: ConversationStore,
activity: BotFrameworkActivity,
bot_app_id: str,
) -> None:
ref = await build_conversation_reference(activity, bot_app_id)
store.set(ref)
async def send_proactive_text(
adapter: BotFrameworkAdapter,
store: ConversationStore,
conversation_id: str,
text: str,
*,
reply_to_id: str | None = None,
) -> list[str]:
ref = store.get(conversation_id)
if not ref:
raise ValueError(f"No conversation reference for: {conversation_id}")
return await send_text(adapter, ref, text, reply_to_id=reply_to_id)
async def send_proactive_media(
adapter: BotFrameworkAdapter,
store: ConversationStore,
conversation_id: str,
media_url: str,
media_type: str,
*,
name: str = "",
) -> str:
ref = store.get(conversation_id)
if not ref:
raise ValueError(f"No conversation reference for: {conversation_id}")
return await send_media(adapter, ref, media_url, media_type, name=name)