代码之家  ›  专栏  ›  技术社区  ›  Kaveesha Baddage

如何在Microsoft botframework webchat的React组件中插入文本?

  •  1
  • Kaveesha Baddage  · 技术社区  · 8 年前

    我想在Microsoft chatbot中添加和发送消息,而无需在Bot窗口底部的textArea中键入该消息。 我需要将消息作为道具提供给react组件(botframework webchat)。

    我尝试了这篇文章中给出的建议 link 这对我来说并不管用。

    我该怎么做?

    我试过这个。

    import { Chat } from "botframework-webchat";
    
    class ChatBox extends Component {
    
    componentDidMount() {
       document.getElementsByClassName('wc-shellinput')[0].value = "testMsg"
       document.getElementsByClassName('wc-send')[0].click()
    }
    
    render() {
       return (
          <React.Fragment>
            <div>
             <Chat
                directLine={{
                   secret:
                   "...."
                }}
                user={{ id: "Test" }}
                bot={{ id: "Demo" }}
                resize={"detect"}
                ref="chatBox"
             />
          </div>
        </React.Fragment>
        )
    }
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Gary Liu    8 年前

    我不擅长React,但我发现在React中,我们只能通过其状态更新输入值。和via source code ,似乎有一个入口函数 onChangeText() 。

    因此,请尝试以下代码段来更新输入值:

      componentDidMount(){
        console.log(this.chat);
        this.chat.shellRef.props.onChangeText('testMsg');      
      }