Search code examples
botframework

v4 Bot sendActivity no output in Bot [object Promise]


I do not get an output inside my web chat Bot with the line

await turnContext.sendActivity('No output in Bot? '+this.translateText(turnContext.activity.text));

There is no error message and in the log I get the correct JSON from the Microsoft Cognitive Text Translator API service. But in the Bot Framework emulator I get only [object Promise]?

const request = require('request');
const uuidv4 = require('uuid/v4');
const rp = require('request-promise');
class EchoBot {
    constructor(conversationState) {
        this.conversationState = conversationState;
    }
    async onTurn(turnContext) {
        if (turnContext.activity.type === ActivityTypes.Message) {
            // OK
            await turnContext.sendActivity(`${ count }: Alex you said "${ turnContext.activity.text }"`);

            // not output in Bot?
            await turnContext.sendActivity('No output in Bot? '+this.translateText(turnContext.activity.text));
        } else {
            await turnContext.sendActivity(`[${ turnContext.activity.type } event detected]`);
        }
        await this.conversationState.saveChanges(turnContext);
    }
    async translateText(inputText){
        let options = {
            method: 'POST',
            baseUrl: 'https://api.cognitive.microsofttranslator.com/',
            url: 'translate',
            qs: {
            'api-version': '3.0',
            'to': 'de'
            },
            headers: {
            'Ocp-Apim-Subscription-Key': subscriptionKey,
            'Content-type': 'application/json',
            'X-ClientTraceId': uuidv4().toString()
            },
            body: [{
                'text': inputText
            }],
            json: true,
        };
        rp(options)
        .then(function (repos) {
            console.log(JSON.stringify(repos, null, 4));
            return JSON.stringify(repos, null, 4);
         })
        .catch(function (err) {
            console.log("error alex");
        });
    }; 
}

Solution

  • Since you are using the response-promise package, I would recommend using async/await instead of the then/catch method. The async/await approach falls more inline with the flow of the BotFramework and will allow you to return the resulting promise from your request to your onTurn method. This is how your translateText function should look:

    async translateText(inputText){
        let options = {
            method: 'POST',
            baseUrl: 'https://api.cognitive.microsofttranslator.com/',
            url: 'translate',
            qs: {
            'api-version': '3.0',
            'to': 'de'
            },
            headers: {
            'Ocp-Apim-Subscription-Key': subscriptionKey,
            'Content-type': 'application/json',
            'X-ClientTraceId': uuidv4().toString()
            },
            body: [{
                'text': inputText
            }],
            json: true,
        };
    
        const repos = await rp(options);
        return repos[0].translations[0].text;
    
    }; 
    

    Note, since translateText is an asynchronous method and returns a promise you will have to add await before the function call.

    await turnContext.sendActivity('No output in Bot? ' + await this.translateText(turnContext.activity.text));
    

    Screenshot

    Hope this helps!