javascript - 如何在 botpress ver 11.9.5 中自定义用户对话框?

标签 javascript css reactjs chatbot botpress

我正在尝试为 botpress 聊天中的每个对话框添加时间戳。到目前为止,我可以在机器人的对话框中添加这个时间戳,但我需要一些指导来将其添加到用户的对话框和选择技能中。

聊天截图显示了机器人对话框中的时间戳 enter image description here 自定义组件

export class InfaText extends React.Component {
  message = this.props.text

  getTimestamp = () => {
    let date = new Date();
    let options = {
      month: "short",
      day: "numeric", hour: "2-digit", minute: "2-digit"
    };
    return date.toLocaleTimeString("en-us", options);
  }
  render() {
    return (<div className="infaTextMain">
      <p className="infaTextMessage">{this.message}</p>
      <small className="infaTextTimestamp">{this.getTimestamp()}</small>
    </div>)
  }
}

注:Botpress v11.9.5

此外,是否有一种通用方法可以为所有对话框添加时间戳? 更新

我完全按照@eff_it 所说的去做了

我将 MessageWrapperMySuperOverride 函数复制到 modules\infa-module\src\views\lite\index.jsx enter image description here

然后在 modules\channel-web\src\views\full\index.tsx 文件的覆盖下添加下面的片段

{
          module: 'infa-module',
          component: 'MySuperOverride'
}

enter image description here

仍然没有效果,@eff_it 请看一下并提出建议,这是这里缺少的东西吗?

enter image description here

最佳答案

你试过 BP 12 了吗?自定义组件现在更容易实现。

您可以使用 botpressWebchat 商店的 setMessageWrapper 包装每条消息,但要这样做,您需要在使用另一个自定义组件初始化网络聊天时使用覆盖属性(不会渲染任何东西,但会使用网络聊天商店)。这是一个示例 views/lite/index.jsx

export const MessageWrapper = props => (
  <div>
    <p style={{ color: 'blue' }}>sent on: {new Date(props.sentOn).toDateString()}</p>
    <div style={{ background: 'black', color: 'white' }}>{props.children}</div>
  </div>
)

export const MySuperOverride = props => {
    props.store.setMessageWrapper({ module: 'yourModule', component: 'MessageWrapper' })
    return null
}

然后当您初始化 botpressWebchat 时,您可以简单地使用覆盖 api,如下所示

window.botpressWebChat.init({
  overrides: {
    before_container: {
      module: 'yourModule',
      component: 'MySuperOverride'
    }
  }
})

引用docs ,有更多关于自定义组件和自定义模块的信息。这是生成的渲染:

botpress custom component rendering

关于javascript - 如何在 botpress ver 11.9.5 中自定义用户对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56818568/

相关文章:

css - 在另一个 div 中居中放置 3 个 div(一个宽度可变,2 个宽度固定)

javascript - 在 React 中将表行添加到表中

javascript - React - 循环遍历多个嵌套数组json对象响应然后更新状态

JavaScript onkeypress 延迟触发

javascript - 使用 JavaScript 对象为 Google 图表创建新对象

css - 如何删除链接,但保持其鼠标悬停效果?

CSS 字体和 'Display'

reactjs - 支架和create-react-app停止工作

java - 遍历 Swing DOM 的最佳方法是什么?

javascript - 从母版页注入(inject)的客户端脚本 block 执行 ASP.NET 回发后运行 Javascript