You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bot Framework v4 sendActivity返回[object Promise]无输出问题求助

Fixing "[object Promise]" Output in Bot Framework When Using Cognitive Translator API

Looks like you're hitting a common async/await pitfall that's causing your bot to show [object Promise] instead of the translated text. Let's break down what's wrong and fix it step by step.

What's Causing the Issue?

  1. You're not awaiting the async function call: translateText is marked as async, which means it returns a Promise by default. When you concatenate it directly into a string like 'No output in Bot? '+this.translateText(...), you're just turning the Promise object into its string representation ([object Promise]) instead of waiting for it to resolve to the actual translated text.
  2. Your translateText function isn't returning the result properly: Inside the function, you're using rp(options).then(...) but you never return the Promise chain. Even if you did await the call, the function would resolve to undefined right now because there's no explicit return value.

Fixed Code

First, let's clean up the translateText function to use proper async/await syntax (it's more readable than chaining .then() calls) and return the actual translated text instead of the full JSON string:

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,
    };
    try {
        const translationResponse = await rp(options);
        console.log(JSON.stringify(translationResponse, null, 4));
        // Extract the actual translated text from the API response
        return translationResponse[0].translations[0].text;
    } catch (err) {
        console.log("Translation error:", err);
        // Return a fallback message if the API call fails
        return "Sorry, I couldn't translate that right now.";
    }
};

Then, update the call in your onTurn method to await the translation before sending it:

async onTurn(turnContext) {
    if (turnContext.activity.type === ActivityTypes.Message) {
        // This works fine since it's a simple string
        await turnContext.sendActivity(`${ count }: Alex you said "${ turnContext.activity.text }"`);
        
        // Wait for the translation to complete before sending
        const translatedText = await this.translateText(turnContext.activity.text);
        await turnContext.sendActivity('Translated to German: ' + translatedText);
    } else {
        await turnContext.sendActivity(`[${ turnContext.activity.type } event detected]`);
    }
    await this.conversationState.saveChanges(turnContext);
}

Quick Notes

  • I adjusted the return value to pull the actual translated text from the API response (the full JSON string isn't useful for end users). The Cognitive Translator API returns an array where each item has a translations array, so translationResponse[0].translations[0].text gives you the German translation directly.
  • Adding a try/catch block ensures your bot doesn't silently fail if the API call runs into issues (like invalid keys or network errors).
  • Double-check that subscriptionKey is properly defined and accessible within the EchoBot class scope.

内容的提问来源于stack exchange,提问作者Alexander Todorovic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:29:07