botonic icon indicating copy to clipboard operation
botonic copied to clipboard

Add event listener to <Message> component that tells when it is rendered

Open YoniSchirris opened this issue 6 years ago • 3 comments

In my specific implementation, I am sending audio over the chatbot. After the audio is done playing, I want to display options to choose from.

It would be nice if Botonic has a pointer to the latest message, which is updated with the state of the message (e.g. 'typing', or 'sent').

Currently, i need to continuously check if the message element has already been rendered by passing it a checkable ID and checking if the document has such an ID every 100 ms, and if so, check the state of the audio component, and depending on that state render the options.

YoniSchirris avatar Feb 04 '20 13:02 YoniSchirris

Hi, does this issue still need fixing? If so then I can give it a try.

Hasankib avatar Mar 22 '21 07:03 Hasankib

So currently I still have the previous workaround in place:

i render with

render(){
<div>
        <CustomAudio id={this.context.session.messageId} src={"data:audio/mp3;base64," + this.context.session.resp.response} />
        {this.context.session.resp.options ? <DisplayClickables audioId={this.context.session.messageId} options={this.context.session.resp.options} /> : null}

</div>
}

with the DisplayClickables class being

class DisplayClickables extends React.Component{
  // React component that will display clickables after the audio has stopped playing

  constructor(props){

    super(props)

    if (props.alwaysShow) {
      this.state = {showOptions: false}

      setTimeout(() => {
        this.setState({showOptions: true})
      }, 2500);

    } else{
      this.state = {showOptions: false,
        audioId: props.audioId,
        audioOptionsId: props.audioId + '_options'
        }
        let return_el = null
        let startTime = new Date()  
        let timerId = setInterval(() => {
    
          if (document.querySelector('#' + this.state.audioId + ' #myAudio')){
            if(Math.floor((Date() - startTime/1000)/60) > 300){ // Stop checking the audio element after 5 minutes.
              clearInterval(timerId)
            }
    
            let audio_el = document.querySelector('#' + this.state.audioId + ' #myAudio') // get the latest posted audio element
            if (audio_el.ended){  // check if it has ended playing, this is a property of the html audio dom element
              clearInterval(timerId)  // then, stop checking
              this.setState({showOptions: true}) // change state to show the clickable options
            }
          }
        }, 800)
    }
  }

where the messageId is manually defined as

 session.messageId = 'a' + uuidv1().split("-").join("") // used to set id, id needs to start with character and can't have '-'

within the botonicInit.

I'm using @botonic/cli/0.18.3 darwin-x64 node-v13.2.0

YoniSchirris avatar Mar 22 '21 08:03 YoniSchirris

Ok, I will attempt something. I will get back to you if I am able to solve the issue.

Hasankib avatar Mar 29 '21 02:03 Hasankib

Hello, I would like to ask this question, is there a solution in the future?

TD-XiPengZhao avatar Feb 13 '23 02:02 TD-XiPengZhao

we'll consider adding this but it is not a priority. feel free to create a PR for this 🤗

vanbasten17 avatar Apr 27 '23 18:04 vanbasten17

Hey ,is it possible for me to work on this? Or if its closed that means it doesn't need to be done?

vbsh123 avatar Jun 24 '23 13:06 vbsh123