javascriptbotkitbotium-box

Botium Botkit 4.0: Specify userId


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.


Solution

  • 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
    ...