Naser Mohd Baig
Naser Mohd Baig

Reputation: 155

Passing variables from webchat client to bot framework

I have a requirement to pass an ID from webchat client to the bot. I did my research and found this stackoverflow post where @justin-harris mentions the following code

const originalDirectline = props.webchat.createDirectLine({
    token,
})

const directLine = Object.assign({}, originalDirectline, {
    postActivity: (activity: any) => {
        const newActivity = Object.assign({}, activity)
        newActivity.customParam = "custom value"
        return originalDirectline.postActivity(newActivity)
    }
})

This works in a sense and I am able to receive the ID in my bot but whenever I send a message from my webchat client as a user, it goes twice (the first message fails and the second succeeds). I am not sure as to why it is doing that. Would really appreciate if somebody sheds some light on the issue.

Upvotes: 1

Views: 617

Answers (1)

Naser Mohd Baig
Naser Mohd Baig

Reputation: 155

Nevermind, I figured it out via a middleware that we can pass during store initialization. Here's the solution for any future souls:

const store = useMemo(
() =>
  createStore({}, ({ dispatch }) => (next) => (action) => {
    if (action.type === "DIRECT_LINE/POST_ACTIVITY") {
      action = simpleUpdateIn(
        action,
        ["payload", "activity", "channelData", "yourVariableHere"],
        () => "Your data here"
      );
    }
    return next(action);
  }),
[]
);

I am using a package called simple-update-in to modify the object. You should be able to access it on the bot end via action.payload.activity.channelData

Upvotes: 1

Related Questions