-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathform_activity.py
More file actions
61 lines (45 loc) · 2.16 KB
/
form_activity.py
File metadata and controls
61 lines (45 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import asyncio
import logging.config
from pathlib import Path
from symphony.bdk.core.activity.command import CommandActivity, CommandContext
from symphony.bdk.core.activity.form import FormReplyActivity, FormReplyContext
from symphony.bdk.core.config.loader import BdkConfigLoader
from symphony.bdk.core.service.message.message_service import MessageService
from symphony.bdk.core.symphony_bdk import SymphonyBdk
async def run():
async with SymphonyBdk(BdkConfigLoader.load_from_symphony_dir("config.yaml")) as bdk:
bdk.activities().register(SlashGifCommandActivity(bdk.messages()))
bdk.activities().register(ReplyFormReplyActivity(bdk.messages()))
await bdk.datafeed().start()
class SlashGifCommandActivity(CommandActivity):
def __init__(self, messages: MessageService):
self._messages = messages
def matches(self, context: CommandContext) -> bool:
return context.text_content.startswith("@" + context.bot_display_name + " /gif")
async def on_activity(self, context: CommandContext):
await self._messages.send_message(context.stream_id, load_gif_elements_form())
class ReplyFormReplyActivity(FormReplyActivity):
def __init__(self, messages: MessageService):
self.messages = messages
def matches(self, context: FormReplyContext) -> bool:
return (
context.form_id == "gif-category-form"
and context.get_form_value("action") == "submit"
and context.get_form_value("category")
)
async def on_activity(self, context: FormReplyContext):
category = context.get_form_value("category")
await self.messages.send_message(
context.source_event.stream.stream_id,
"<messageML> You just submitted this category: " + category + "</messageML>",
)
def load_gif_elements_form():
return (Path(__file__).parent.parent / "resources/gif.mml.xml").read_text(encoding="utf-8")
logging.config.fileConfig(
Path(__file__).parent.parent / "logging.conf", disable_existing_loggers=False
)
try:
logging.info("Running activity example...")
asyncio.run(run())
except KeyboardInterrupt:
logging.info("Ending activity example")