Argument 1: cannot convert from 'string' to 'Microsoft.Bot.Builder.ITurnContext'

177 Views Asked by At

I'm using QNA maker in my chatbot project using Bot Framework and I want to take the question from the adaptive card and send it to the QNA maker but I'm getting an error says: Argument 1: cannot convert from 'string' to 'Microsoft.Bot.Builder.ITurnContext'

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
    {
        var value  = turnContext.Activity.Value;
        if (value != null)
        {
            var val = value.ToString();
            var relationsJSON = JToken.Parse(val);  
            var text = relationsJSON["text"].ToString();
            if (text == "car model")
            {
                var result = await QnAMaker.GetAnswersAsync(text);
                await turnContext.SendActivityAsync(result[0].Answer, cancellationToken: cancellationToken); 
            }
        }
}

the error message on: var result = await QnAMaker.GetAnswerAsnyc(text);

1

There are 1 best solutions below

0
Rajeesh Menoth On

The QnAMaker.GetAnswerAsnyc method is always expecting the ITurnContext as a mandatory parameter. So you can't inject any other parameter in the respective method.

So try the below code here JSON text is assigned to turnContext.Activity.Text object. Because as per your code OnTurnAsync method is always validating turnContext.Activity.Value object.

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
{
    var value  = turnContext.Activity.Value;
    if (value != null)
    {
        var val = value.ToString();
        var relationsJSON = JToken.Parse(val);  
        turnContext.Activity.Text = relationsJSON["text"].ToString();
        if (turnContext.Activity.Text == "car model")
        {
            var result = await QnAMaker.GetAnswersAsync(turnContext.Activity.Text);
            await turnContext.SendActivityAsync(result[0].Answer, cancellationToken: cancellationToken); 
        }
    }
}