2

How can I specify userId in Botium connector for Botkit 4.0?

In 0.7, I was able to specify BOTKIT_USERID in my .spec.js file.

It was useful to test responses for different user groups.

Florian Treml
  • 1,315
  • 2
  • 7
  • 10
shawnl
  • 1,861
  • 1
  • 15
  • 17

1 Answers1

3

The Botium Botkit 4.x Connector is based on the Botium Generic HTTP/JSON Connector, so it is possible to fully customize the payload, including the user id.

By default, the message payload is composed of the message text and a unique generated user id:

{ "text": "{{msg.messageText}}", "user": "{{botium.conversationId}}", "type": "message"}

You can change this in your botium.json:

...
"BOTKIT_4_0_BODY_TEMPLATE": "{ \"text\": \"{{msg.messageText}}\", \"user\": \"my-user-id\", \"type\": \"message\"}",
...

Or you could as well use the UPDATE_CUSTOM logic hook to have a different user id for each of your test cases - mytestcase.convo.txt:

my test case

#begin
UPDATE_CUSTOM BOTKIT_USER_ID|1234567

#me
hallo ...
...

And in your botium.json:

...
"BOTKIT_4_0_BODY_TEMPLATE": "{ \"text\": \"{{msg.messageText}}\", \"user\": \"{{msg.BOTKIT_USER_ID}}{{^msg.BOTKIT_USER_ID}}my-default-user-id{{/msg.BOTKIT_USER_ID}}\", \"type\": \"message\"}",
...

UPDATE

Instead of literal string with all the escape characters you can also use literal JSON in botium.json for specifying the body template:

...
"BOTKIT_4_0_BODY_TEMPLATE": {
    "text": "{{msg.messageText}}", 
    "user": "my-user-id",
    "type": "message"
},
...

Easier to read. Depends on your setup.

UPDATE 2

To use the UPDATE_CUSTOM logic hook in the #begin section there is a pull request outstanding. For now, you can only use the UPDATE_CUSTOM in the #me section, and to use the user id you will have to repeat this for every #me section:

my test case

#me
hallo
UPDATE_CUSTOM BOTKIT_USER_ID|1234567
...
Florian Treml
  • 1,315
  • 2
  • 7
  • 10
  • Seriously, a template for JSON? What's wrong with using JSON.stringify? – Naktibalda Jan 06 '20 at 16:25
  • nothing is wrong with json.stringify. but somehow the conversation data has to be merged into the json data, and using mustache templating is one of the best options to do so. any ideas for alternatives ? – Florian Treml Jan 06 '20 at 18:15
  • Perfect. But `UPDATE_CUSTOM` didn't work for me. It just return 'my-default-user-id'. – shawnl Jan 07 '20 at 06:15
  • Sorry, this was too early. It is part of this PR https://github.com/codeforequity-at/botium-core/pull/444 that UPDATE_CUSTOM can be used in the #begin section. you can use it in the #me section only for now - updating my response. – Florian Treml Jan 07 '20 at 16:22