PowerApps中Chatbot连接报错求助:GenerateAnswer参数数量无效
Hey there, let's tackle that frustrating "Invalid number of arguments" error you're hitting when triggering the button's OnSelect event. The root issue is a mismatch in how you're calling the QnA Maker connector's GenerateAnswer function—let's break this down step by step.
First, Let's Recap Your Setup
Your screen OnStart code looks solid for initializing the chat:
ClearCollect(chat,{id:0,name:"EmployeeFaqBot",text:"Welcome " &User().FullName &" ! Enter your Query"})
The problem lies in how you're invoking OPMS_Service.GenerateAnswer in your button code.
Why Your Original Code Fails
Your initial button OnSelect code includes an extra, unnecessary parameter:
Collect(chat,{id:1,name:User().FullName,text:TextInput1.Text}); Collect(chat,{id:0,name:"EmployeeFaqBot",text:First(OPMS_Service.GenerateAnswer("a6*****7c","application/json",{question:TextInput1.Text}).answers).answer})
The QnA Maker connector's GenerateAnswer function only expects two required parameters:
- Your QnA Knowledge Base ID (the
"a6*****7c"value you already have) - A request object containing the
questionkey
The "application/json" string you added is redundant—PowerApps handles the content type automatically for the connector, so passing it here causes the "invalid number of arguments" error.
The Corrected Button OnSelect Code
Replace your button code with this version, which removes the extra parameter and follows the correct function structure:
Collect(chat, {id: 1, name: User().FullName, text: TextInput1.Text}); Collect(chat, {id: 0, name: "EmployeeFaqBot", text: First(OPMS_Service.GenerateAnswer("a6*****7c", {question: TextInput1.Text}).answers).answer})
What Was Wrong With Your Other Attempts
Let's quickly go over why your alternate code snippets didn't work:
- Attempt 1 (missing Knowledge Base ID):
GenerateAnswer({question:TextInput1.Text})omits the first required parameter (your KB ID), so the function doesn't know which QnA dataset to query. - Attempt 2 (swapped parameters):
GenerateAnswer("application/json",{question:TextInput1.Text})uses the content type string as the KB ID, which is an invalid value and mismatches the expected parameter type. - Attempt 3 (invalid object syntax):
GenerateAnswer({"application/json",question:TextInput1.Text})uses broken object syntax (you can't list values without keys) plus still includes the unnecessary content type.
Bonus: Add Robust Error Handling
To make your chat more user-friendly, wrap the bot response in an IfError block to handle cases where the QnA service might fail:
Collect(chat, {id: 1, name: User().FullName, text: TextInput1.Text}); IfError( Collect(chat, {id: 0, name: "EmployeeFaqBot", text: First(OPMS_Service.GenerateAnswer("a6*****7c", {question: TextInput1.Text}).answers).answer}), Collect(chat, {id: 0, name: "EmployeeFaqBot", text: "Sorry, I couldn't process your query right now. Please try again later."}) )
内容的提问来源于stack exchange,提问作者Jyoti Sharma

