部署的机器人获取响应时出现问题;

huangapple go评论78阅读模式
英文:

Problems with Getting Response on the deployed bot;

问题

I've got some problems with getting REST API response that allows to get members of the group( GET {serviceUrl}/v3/conversations/{conversationId}/members). I'm debugging my bot in Bot Emulator, and it runs fine here

部署的机器人获取响应时出现问题;

  • that's what it shows in the Bot Simulator

部署的机器人获取响应时出现问题;

  • But that's how it shows on Azure.

The main problem is that I really can't recognize the problem. I know that the problem is in requesting because the getResponse method crashes the whole program when it is on the server (but why does this work in the bot emulator?). Here is the code (I'm using .NET CORE, Microsoft Bot Framework). P.S. {id} in Authorization is correct, and I get it from another request (but unlike the problem request, that request works everywhere), the {url} is turnContext.Activity.ServiceUrl

HttpWebRequest webRequest23 = (HttpWebRequest)WebRequest.Create($"{url}/v3/conversations/{turnContext.Activity.Conversation.Id}/members");

await turnContext.SendActivityAsync($"{url}/v3/conversations/{turnContext.Activity.Conversation.Id}/members");

await turnContext.SendActivityAsync(turnContext.Activity.Conversation.Id);

webRequest23.ContentType = "application/json";
webRequest23.Headers["Authorization"] = $"Bearer {id}";
WebResponse response2 = webRequest23.GetResponse(); // crash here
await turnContext.SendActivityAsync("3");

string smth25 = "";
using (StreamReader stream = new StreamReader(response2.GetResponseStream(), true))
{
    smth25 = stream.ReadToEnd();
}
JArray jsonArray = JArray.Parse(smth25);
List<string> names = new List<string>();
foreach (var x in jsonArray)
{
    var name = JObject.Parse(x.ToString())["name"].ToString();
    names.Add("@" + name);
}
string s = "This group consists of - ";
foreach (var x in names)
{
    s += x + ",";
}
await turnContext.SendActivityAsync(s);

Here is how I get id:

string stringData = "grant_type=client_credentials&client_id=66b16ef5-d086-40b7-ae9c-2f50a1f028c6&client_secret=yRUYg4g.:ANuw3Y_01V=@.JkAv=@g3EE&scope=https%3A%2F%2Fapi.botframework.com%2F.default";
var data = Encoding.Default.GetBytes(stringData);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token");
webRequest.Host = "login.microsoftonline.com";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
var newStream = webRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
WebResponse response = webRequest.GetResponse();
string line = string.Empty;

string smone = "";
using (StreamReader stream = new StreamReader(response.GetResponseStream(), true))
{
    smone = stream.ReadToEnd();
}
var id = JObject.Parse(smone)["access_token"].ToString();
英文:

I've got some problems with getting REST API response that allows to get members of the group( GET {serviceUrl}/v3/conversations/{conversationId}/members). I'm debugging my bot in Bot Emulator, and it runs fine here

部署的机器人获取响应时出现问题;

  • that's what it shows in the Bot Simulator

部署的机器人获取响应时出现问题;

  • But that's how it shows on Azure.

The main problem is that i really can't recognize the problem.I know that the problem is in requesting because the getResponse method crash all programm when it is on server(but why this works in the bot emulator?).Here is the code(I'm using .NET CORE, Microsoft Bot Framework). P.S.{id} in Authorization is correct, and i get it from another request(but unlike the problem request that request works everywhere), the {url} is turnContext.Activity.ServiceUrl

HttpWebRequest webRequest23 = (HttpWebRequest)WebRequest.Create($&quot;{url}/v3/conversations/{turnContext.Activity.Conversation.Id}/members&quot;);

await turnContext.SendActivityAsync($&quot;{url}/v3/conversations/{turnContext.Activity.Conversation.Id}/members&quot;);

await turnContext.SendActivityAsync(turnContext.Activity.Conversation.Id);

webRequest23.ContentType = &quot;application/json&quot;;
webRequest23.Headers[&quot;Authorization&quot;] = $&quot;Bearer {id}&quot;;
WebResponse response2 = webRequest23.GetResponse();//crash here
await turnContext.SendActivityAsync(&quot;3&quot;);

string smth25 = &quot;&quot;;
using (StreamReader stream = new StreamReader(response2.GetResponseStream(), true))
{
        smth25 = stream.ReadToEnd();
}
JArray jsonArray = JArray.Parse(smth25);
List&lt;string&gt; names = new List&lt;string&gt;();
foreach(var x in jsonArray)
{
         var name = JObject.Parse(x.ToString())[&quot;name&quot;].ToString();
         names.Add(&quot;@&quot; + name);
}
         string s = &quot;This group consists of - &quot;;
         foreach (var x in names){
                
         s += &quot;&quot; + x + &quot;,&quot;;
}
await turnContext.SendActivityAsync(s);

Here is how i get id:

string stringData = &quot;grant_type=client_credentials&amp;client_id=66b16ef5-d086-40b7-ae9c-2f50a1f028c6&amp;client_secret=yRUYg4g.:ANuw3Y_01V=@.JkAv=@g3EE&amp;scope=https%3A%2F%2Fapi.botframework.com%2F.default&quot;;
var data = Encoding.Default.GetBytes(stringData);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(&quot;https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token&quot;);
webRequest.Host = &quot;login.microsoftonline.com&quot;;
webRequest.ContentType = &quot;application/x-www-form-urlencoded&quot;;
webRequest.Method = &quot;POST&quot;;
var newStream = webRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
WebResponse response = webRequest.GetResponse();
string line = string.Empty;

string smone = &quot;&quot;;
using (StreamReader stream = new StreamReader(response.GetResponseStream(), true))
{
    smone = stream.ReadToEnd();
}
var id = JObject.Parse(smone)[&quot;access_token&quot;].ToString();

答案1

得分: 1

我不明白你的 "turnContext.SendActivityAsync" 行到底是要做什么,但它可能在机器人仿真器中起作用,因为仿真器不像Teams一样需要正确的经过身份验证的会话。无论如何,像这样的代码应该能帮助你:

首先,在某处定义这个类:

public class MicrosoftTeamUser
{
    public string Id { get; set; }

    public string ObjectId { get; set; }

    public string GivenName { get; set; }

    public string Surname { get; set; }

    public string Email { get; set; }

    public string UserPrincipalName { get; set; }

    public string TenantId { get; set; }
}

然后使用以下代码:

List<ChannelAccount> teamMembers = (await turnContext.TurnState.Get<IConnectorClient>().Conversations.GetConversationMembersAsync(
                turnContext.Activity.GetChannelData<TeamsChannelData>().Team.Id).ConfigureAwait(false)).ToList();

List<MicrosoftTeamUser> teamsUsers = new List<MicrosoftTeamUser>();
foreach (var item in teamMembers)
{
    var teamsUser = JsonConvert.DeserializeObject<MicrosoftTeamUser>(item.Properties.ToString());
    teamsUser.Id = item.Id;
    teamsUsers.Add(teamsUser);
}

然后,你将在 teamMembers 变量中获得团队成员的列表。

英文:

I don't understand exactly what your "turnContext.SendActivityAsync" lines are supposed to do, but it might be working in the bot emulator because the emulator doesn't require a proper authenticated session like Teams does. Anyway, something like this should actually help you:

First, define this class somewhere:

public class MicrosoftTeamUser
{
    public string Id { get; set; }

    public string ObjectId { get; set; }

    public string GivenName { get; set; }

    public string Surname { get; set; }

    public string Email { get; set; }

    public string UserPrincipalName { get; set; }

    public string TenantId { get; set; }
}

then use:

List&lt;ChannelAccount&gt; teamMembers = (await turnContext.TurnState.Get&lt;IConnectorClient&gt;().Conversations.GetConversationMembersAsync(
                turnContext.Activity.GetChannelData&lt;TeamsChannelData&gt;().Team.Id).ConfigureAwait(false)).ToList();

            List&lt;MicrosoftTeamUser&gt; teamsUsers = new List&lt;MicrosoftTeamUser&gt;();
            foreach (var item in teamMembers)
            {
                var teamsUser = JsonConvert.DeserializeObject&lt;MicrosoftTeamUser&gt;(item.Properties.ToString());
                teamsUser.Id = item.Id;
                teamsUsers.Add(teamsUser);
            }

Then you'll have the list of members in the teamMembers variable

huangapple
  • 本文由 发表于 2020年1月4日 01:31:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定