Search code examples
botframework

automating microsoft bot testing


I am trying to come up with a way to automate the testing of a bot im making with the bot framework. I have used the emulator which is great, but I am wanting to automate things a bit. From my research I have found botium which seems like a great tool, but i get pretty lost when looking through the documentation. I have also heard about using the directline api. Again though, my lack of understanding is getting in the way. Does anyone know if there is a way to connect to the bot using REST? for example, my ideal scenario would be to use a csv file to feed inputs and expected outputs for the bot. I would like to cycle through the file, passing an input to the bot and checking if the response matches my expected output. I am thinking about doing this in powershell because I have some experience there, but im not sure how to start. Any guidance would be much appreciated.


Solution

  • Does anyone know if there is a way to connect to the bot using REST?

    You can use the Direct Line API to send/receive activities to/from the bot.

    my ideal scenario would be to use a csv file to feed inputs and expected outputs for the bot. I would like to cycle through the file, passing an input to the bot and checking if the response matches my expected output. I am thinking about doing this in powershell because I have some experience there, but im not sure how to start.

    Based on you requirement, I write the following scripts to achieve it, you can refer to it.

    $directline_secert = "your_directline_secret"
    $conversation_Id = ""
    $filepath = "d:\testmes.csv"
    $botId = "You_bot_id"
    
    
    function StartConversation([string]$directlinesecert){
        $uri = "https://directline.botframework.com/v3/directline/conversations"
    
        $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
        $headers.Add("Authorization", "Bearer " + $directlineSecert)
    
        $response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers
        $global:conversation_Id = $response.conversationId
    
        Write-Host ("Conversation Id: " + $response.conversationId)
    }
    
    function SendActivity([string]$directlineSecert, [string]$conversationId, [string]$mes){
        $uri = "https://directline.botframework.com/v3/directline/conversations/" + $conversationId + "/activities"
    
        $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
        $headers.Add("Authorization", "Bearer " + $directlineSecert)
        $headers.Add("Content-Type", "application/json")
    
        $Body = @{
        "type" = "message"
        "from" = @{"id" ="user1"}
        "text" = $mes
        } | ConvertTo-Json
    
        $response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $Body -ContentType 'application/json'
    
    
        Write-Host ("Id: " + $Response.id)
    }
    
    function ReceiveActivities([string]$directlineSecert, [string]$conversationId){
        $uri = "https://directline.botframework.com/v3/directline/conversations/" + $conversationId + "/activities"
    
        $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
        $headers.Add("Authorization", "Bearer " + $directlineSecert)
    
    
        $response = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers
    
    
        #Write-Host ("Activities number: " + $Response.activities.Count)
    
        Foreach($activity in $Response.activities){
          if($activity.type -eq "message" -and $activity.from.id -eq $botId){
    
            #your code logic to compare received activities with expected result
            Write-Host ("Activities Mes: " + $activity.text)
          }     
        }
    }
    
    StartConversation -directlineSecert $directline_secert
    
    
    
    #SendActivity -directlineSecert $directline_secert -conversationId '5e5JeKRtG5vBnGIhdip1Hv' -mes 'hi bot' 
    
    
    Import-CSV $filepath -Header Mes | Foreach-Object{
       #Write-Host $_.Mes
    
       SendActivity -directlineSecert $directline_secert -conversationId $conversation_Id -mes $_.Mes 
    }
    
    ReceiveActivities -directlineSecert $directline_secert -conversationId $conversation_Id
    

    Test result:

    enter image description here