简体   繁体   中英

Sending Message from Botframework to Azure Test WebChat

Im currently trying to send a message from the Bot Framework Emulator to the Test Web Chat in Azure. My Problem is when trying this:

var my2Response = await myClient.SendAsync(request).ConfigureAwait(false);

I get StatusCode: 403, ReasonPhrase: 'Forbidden', and I don't know what I should do here. I noticed that request.Content also has Headers and tried this crazy thing: request.Content.Headers.Add("Authorization", "Bearer " + myToken);

but as you might guessed it, this is not the solution. How can I send a post to my Azure Test Webchat via Botframework? (if that works I want to try to send a message to the Test Webchat via MS Teams)

Here is the related code:

           HttpClient myClient = new HttpClient();
            myClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            var myToken = token;
            myClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", myToken);

            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = new System.Uri("https://webchat.botframework.com/v3/conversations/conversationID/activities"),
                Content = new StringContent("{\"type\": \"message\", \"text\": \"I come from teams\", \"from\": {\"id\": \"bot@somthing\", \"name\": \"teams\"}}", System.Text.Encoding.UTF8),

            };
            request.Content.Headers.ContentType.MediaType = "application/json";
            request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.Add("Authorization", "Bearer " + myToken);
            var my2Response = await myClient.SendAsync(request).ConfigureAwait(false);
            my2Response.EnsureSuccessStatusCode();
            var myResponseBody = await my2Response.Content.ReadAsStringAsync().ConfigureAwait(false);

edit

The token I'm using is generated from here: https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token

Changed my Code, now it works:

            HttpClient myClient = new HttpClient();
            myClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            var myToken = token;
            using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://webchat.botframework.com/v3/conversations/ConversationID/activities"))
            {
                requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", myToken);
                requestMessage.Content = new StringContent("{\"type\": \"message\", \"text\": \"I come from teams\", \"from\": {\"id\": \"bot@someID\", \"name\": \"teams\"}}", System.Text.Encoding.UTF8, "application/json");
                var myResponse = await myClient.SendAsync(requestMessage);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM