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?
- You're not awaiting the async function call:
translateTextis marked asasync, 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. - Your
translateTextfunction isn't returning the result properly: Inside the function, you're usingrp(options).then(...)but you never return the Promise chain. Even if you did await the call, the function would resolve toundefinedright 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
translationsarray, sotranslationResponse[0].translations[0].textgives you the German translation directly. - Adding a
try/catchblock ensures your bot doesn't silently fail if the API call runs into issues (like invalid keys or network errors). - Double-check that
subscriptionKeyis properly defined and accessible within theEchoBotclass scope.
内容的提问来源于stack exchange,提问作者Alexander Todorovic
相关产品推荐
相关产品推荐

