聊天会话开始时将参数传递给botframework

时间:2020-02-03 12:49:08

标签: botframework

我找不到在会话开始时将参数传递给bot框架的方法。 我想在用户开始聊天时传递projectId,并且必须自动传递它(即,不得提示用户输入此信息)。

几天以来,我一直在尝试数十种方法,但没有任何效果。 我有两种接近结果的方法:

var projectId ="191";
var d1 = window.WebChat.createDirectLine({ token });
    		
const store = window.WebChat.createStore(
  {},
  function() {
  return function(next) {
  return function(action) {
  if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
  action = window.simpleUpdateIn(
  action,
  ['payload', 'activity', 'channelData'],
  () => ({
    	'email': "testemail1@test.com",
    	'projectId': projectId
    	})
    	)
     	}
   return next(action);
  }
 }
);

window.WebChat.renderWebChat({
    directLine: d1,
    store: store,
    styleOptions:styleOptions
}, document.getElementById('webchat'));		
    		

var user = {
    id: 'default-user',
    name: 'user name'
};
    		
var activity = {
    from: user,
    name: 'startConversation',
    type: 'event',
    value: '',		
    channelData: {  "email": "test2@b.com","usertoken": userToken,"projectId":projectId}		
    };

    d1.postActivity(activity).subscribe(function(id) {
    if (console) {
    	console.log('"trigger requestWelcomeDialog" sent');
    }
});

  1. 如果我尝试通过商店和DIRECT_LINE / POST_ACTIVITY传递参数,则在创建会话后传递参数(为时已晚) 2.如果我通过了typeEvent的postActivity,我设法覆盖OnEventAsynxc并访问参数。但是我不明白如何将它们传递给对话框。 在对话框中,当我转储channelData和对话状态或用户状态时,它为空。

请帮助,我只需要在聊天开始时自动传递projectID。 看起来微不足道,但事实并非如此。

    protected override async Task OnEventAsync(ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
    {
        IStatePropertyAccessor<OnboardingState> accessor = UserState.CreateProperty<OnboardingState>(nameof(OnboardingState));
        OnboardingState state = await accessor.GetAsync(turnContext, () => new OnboardingState());
        state.Ticket = new CIWTicket();
        state.Ticket.SourceEmailAddress = email;
        await UserState.SaveChangesAsync(turnContext, false, cancellationToken);
    }

1 个答案:

答案 0 :(得分:1)

您可以使用dispatch()通过发送事件和存储在有效负载中的数据来实现。如下所述,dispatch()会被调用,并在直线结束与机器人的连接(“ CONNECTION_FULFILLED”)时发送事件。

<script>
  (async function () {
    const store = window.WebChat.createStore( {}, ( { dispatch } ) => next => async action => {
      if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
        console.log('Event dispatched');
        dispatch( {
          type: 'WEB_CHAT/SEND_EVENT',
          payload: {
            name: 'SEND_PROJECT_ID',
            value: 191
          }
        } )
      }

      return next(action);
    });

    [...]

    window.ReactDOM.render(
      <ReactWebChat
      directLine={ window.WebChat.createDirectLine({ token }) }
      store={store}
      />,
    document.getElementById( 'webchat' );
    )
  })
</script>

开发者控制台

enter image description here

由机器人记录

enter image description here

希望有帮助!