@twilio/flex-sdk
    Preparing search index...

    Interface Conversation

    Interface for managing a Twilio Conversation.

    To listen for events in a particular conversation, we can use the conversation.conversation property, which is an instance of TwilioConversation.

    import { createClient, SendTextMessageOptions } from "@twilio/flex-sdk";
    import { AddConversationEventListener } from "@twilio/flex-sdk/actions/Conversation";

    async function subscribeToNewMessages() {
    const client = await createClient("SDK_TOKEN");
    const conversationListener = new AddConversationEventListener("conversationAdded", (conversation) => {
    conversation.conversation.on("messageAdded", (message) => {
    console.log(`New message in conversation ${conversation.sid}:`, message);
    });
    const messageOptions: SendTextMessageOptions = {
    body: "Hello, world!"
    };
    await conversation.sendMessage(messageOptions);
    });
    const { unsubscribe } = await client.execute(conversationListener);
    }
    interface Conversation {
        conversation: TwilioConversation;
        sid: string;
        getMessages(
            pageSize?: number,
            anchorMessageIndex?: number,
            direction?: MessagesDirection,
        ): Promise<Paginator<Message>>;
        sendMessage(
            messageOptions: SendTextMessageOptions | SendEmailMessageOptions,
        ): Promise<null | number>;
        sendTyping(): Promise<void>;
    }
    Index

    Properties

    conversation: TwilioConversation
    sid: string

    Methods

    • Get a list of messages from a conversation.

      Parameters

      • OptionalpageSize: number

        number of messages per page.

      • OptionalanchorMessageIndex: number

        message index to rely on when fetching the messages. Depending on direction will either get messages until, or after this anchorMessageIndex index.

      • Optionaldirection: MessagesDirection

        direction in which the messages will be sorted - last to first or vice versa.

      Returns Promise<Paginator<Message>>

      Pages with messages.

      import { createClient, AddConversationEventListener } from "@twilio/flex-sdk";

      const client = await createClient("SDK_TOKEN");

      const addConversationEventListener = new AddConversationEventListener("conversationAdded", (conversation) => {
      conversation.getMessages();
      })
      client.execute(addConversationEventListener)
    • Send that worker is currently typing

      Returns Promise<void>

      Promise which resolves when the operation is completed

      import { createClient } from "@twilio/flex-sdk";
      import { GetConversationByTask } from "@twilio/flex-sdk/actions/Conversation";

      const client = await createClient("SDK_TOKEN");
      const getConversationByTask = new GetConversationByTask(
      "WTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      );
      const conversation = await client.execute(getConversationByTask);
      await conversation.sendTyping();