Unable to send message to my bot using direct line

39 views Asked by At

I have deployed my bot (sdk v4.22) to Azure. Its a simple echo bot. When I use bot's inbuilt message field to send message it get Echo response but when I send the message via direct line from my client side vb.net code I do get a successful sendMessageResponse but my message does not appear in web chat.

Dim directLineSecret As String = adb.GetLiteralParameterValue("Param_adb_AI_Config_Azure_DirectLine_AS_Ext_Key").Trim
    Using client As New HttpClient()

        ' Generate a token using the Direct Line secret
        client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", directLineSecret)
        Dim tokenResponse = client.PostAsync("https://directline.botframework.com/v3/directline/tokens/generate", Nothing).Result
        
        If tokenResponse.IsSuccessStatusCode Then
            Dim tokenContent = tokenResponse.Content.ReadAsStringAsync().Result
            Dim tokenData = JObject.Parse(tokenContent)
            Dim token As String = tokenData("token").ToString()

            ' Now use this token for Authorization instead of the secret
            client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", token)

            ' Start a new conversation
            Dim conversationResponse = client.PostAsync("https://directline.botframework.com/v3/directline/conversations", Nothing).Result
            If conversationResponse.IsSuccessStatusCode Then
                Dim conversationContent = conversationResponse.Content.ReadAsStringAsync().Result
                Dim conversationData = JObject.Parse(conversationContent)
                Dim conversationId As String = conversationData("conversationId").ToString()

                ' Send a message to the conversation
'                Dim userId As String = $"dl_{Guid.NewGuid()}"
                Dim userId As String = "You"
                Dim messageJson As String = JsonConvert.SerializeObject(New With {
                    Key .type = "message",
                    Key .from = New With {Key .id = userId},
                    Key .text = "Hello, bot!"
                })
                Dim messageContent = New StringContent(messageJson, Encoding.UTF8, "application/json")
                Dim sendMessageEndpoint As String = $"https://directline.botframework.com/v3/directline/conversations/{conversationId}/activities"
                Dim sendMessageResponse = client.PostAsync(sendMessageEndpoint, messageContent).Result

                If Not sendMessageResponse.IsSuccessStatusCode Then
                    adb.Ex("Failed to send message.")
                    Dim errorContent = sendMessageResponse.Content.ReadAsStringAsync().Result
                    adb.Ex($"Error content: {errorContent}")
                Else
                    adb.Ex("Message sent successfully.")
                End If
            Else
                adb.Ex("Failed to start conversation.")
            End If
        Else
            adb.Ex("Failed to generate token.")
        End If
    End Using

FYI, I am using this URL in my application to see web chat interface: https://webchat.botframework.com/embed/My-AI-Bot?s=mySecret&username=You

0

There are 0 answers