Twilio FlexWebchat“sendMessage”触发消息两次

MET*_*EAD 5 twilio twilio-flex twilioflexwebchat

我正在使用 Twilio Flex WebChat 发送和接收消息。我需要在发送消息之前修改消息。因此,我添加了一个侦听器beforeSendMessage,在componentDidMount()其中收集消息正文、转换消息并发送消息。这里的问题是它同时发送原始消息和转换后的消息。我的目标是单独发送转换后的消息。你能帮我一下吗?谢谢。

    componentDidMount() {
        FlexWebChat.Actions.addListener(
          'beforeSendMessage',
          async (payload) => {
            const { body, channelSid } = payload;
            const modifiedBody = transform(body)  //Transforming the message here
            await FlexWebChat.Actions.invokeAction('SendMessage', {
              body: modifiedBody,  // Sending the transformed message
              channelSid,
            })
          }
        )
     }
Run Code Online (Sandbox Code Playgroud)

小智 1

发生这种情况的原因是因为您执行了两次 SendMessage。

你可以用Listener做的就是修改payload并让执行继续,它将继续执行。如果您想阻止消息发送,您可以拨打电话abortFunction()

  componentDidMount() {
    FlexWebChat.Actions.addListener(
      'beforeSendMessage',
      async (payload, abortFunction) => {
        const { body, channelSid } = payload;
        payload.body = transform(body);  //Transforming the message here
      }
    )
 }
Run Code Online (Sandbox Code Playgroud)