Thanks @PierreSnell , I did try replacing event.preview and event.payload.text variables, however, it didn’t work as expected. I believe I shouldn’t use async call to my custom API in the before_incoming_middleware hook.
I am sharing the snippet of my code below. Please have a look at and guide me through this.
const translate_to_english = async() => {
var input_text = event.payload.text
var data = JSON.stringify({ "text": input_text, "language": "en" })
var headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': true
}
bp.logger.info("inside translate hook")
//config object to call the translation API
var config = {
method: 'post',
url: 'http:URL/language_call_url',
headers: headers,
mode: 'cors',
data: data
};
// axios call to the API
var resp = await axios(config)
try {
bp.logger.info("in success")
bp.logger.info(JSON.stringify(resp.data))
const botId = event.botId
const userId = event.target
bp.logger.info("Resp " + resp.data)
// replacing event.preview with the response of the above API
event.preview = resp.data
} catch (error) {
bp.logger.info("in error")
bp.logger.error(error);
}
}
bp.logger.info("Channel : " + event.channel)
if (event.preview != “Reset the conversation” && event.channel != ‘api’) {
translate_to_english()
bp.logger.info("Preview : " + event.preview)
}