Custom Hook - access value in request header/body

Hi, I am trying to build a chat test capability, Using generic HTTP/JSON interface for this need to call a external API to fetch a conversation id and set in the header/body of the chat bot request to be sent.

For this I am trying to create a custom hook in advanced option to call the API and set the result into the container variable but not sure how to access and set in the bot request headers. Can you please help or provide a sample which I can use to access the field in setting request headers or body. Can I set the result from Custom Hook into context or other variable (Implementing Token-Based Authentication for Test Cases - Botium Wiki)

Also tried to call the API using the Request Hook in the UI, but it throws a error stating “request” function not found when trying to call a external API. Is there a example to show how the request hook can be used to fetch the REST API response value and set into Header or body.

2022-05-17T08:49:55.347Z botium-core-Convo BotiumError: chatbothello/Line 2: error sending to bot - request is not a function

I cannot use the Session Setup configuration option to set the value as I have a separate API call to be made to get token.

{
“Accept”: “application/json”,
“Authorization”: “Bearer {{context.access_token}}”,
“bot”: "##Need to set the value here dynamically, which is fetched from a external API## "
}

Please let know if more information is required

Hi,

Referenced Read variable from external source and set capability through code or any other means - #2 by Florian and found an answer that works for the problem.

In the custom_hook need to set the value into the Plugin instance and when accessing the information in the header or body need to access it from the container variable.

So my code within the custom_hook looks as below,

module.exports = async ({ container, request }) => {
return new Promise((resolve, reject) => {
const requestOptions = {
method: ‘get’,
uri: ‘https://jsonplaceholder.typicode.com/todos/1’,
json: true
}
request(requestOptions, (err, response, body) => {
if (err) return reject(err)
container.pluginInstance.caps.MYTOKEN = body.title
resolve()
})
})
}

Accessing the custom set token using the mustache variable {{container.caps.MYTOKEN}} within the header or body section.

1 Like