Skip to content

[feature] Add support for generators returned by commands#71

Merged
fbradyirl merged 1 commit into
fbradyirl:mainfrom
eforbus:main
Apr 24, 2025
Merged

[feature] Add support for generators returned by commands#71
fbradyirl merged 1 commit into
fbradyirl:mainfrom
eforbus:main

Conversation

@eforbus

@eforbus eforbus commented Apr 24, 2025

Copy link
Copy Markdown
Collaborator

Simple change to allow commands to return generators instead of a list of responses allowing the bot to stream responses back as they arrive from a source.

Example of what a command might look llike:

import logging
from webex_bot.models.response import Response

log = logging.getLogger(__name__)


class StreamCommand(Command):

    def __init__(self):
        super().__init__(
            command_keyword="stream",
            help_message="Stream Stuff Back to You!",
        )

    def execute(self, message, attachment_actions, activity):
        """
        If you want to respond to a submit operation on the card, you
        would write code here!

        You can return text string here or even another card (Response).

        This sample command function simply echos back the sent message.

        :param message: message with command already stripped
        :param attachment_actions: attachment_actions object
        :param activity: activity object

        :return: a string or Response object (a list or generator of either). Use Response if you want to return another card.
        """

        client = get_api_or_db_client()

        # a long running query that might stream messages back to the client
        for message in client.run_query():

            yield Response(attributes={"markdown": message, "roomId": None, "parentId": None})

@fbradyirl fbradyirl merged commit 5a8c224 into fbradyirl:main Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants