我是在Azure.NET中学习Bot框架的初学者,我遇到了这个机器人样本(https://github.com/tompaana/intermediator-bot-sample),该例子完美地演示了人为交接的情况。我可以在本地主机上执行它,并且按预期工作。
当我通过Azure Bot服务在MS Teams频道上发布此POC时,代码向我返回了异常“操作返回了无效的状态代码” badrequest”。在团队中键入“ @Bot GetRequests”时尤其如此。 (此命令用于在人工切换方案中检索待处理的已接受请求。)
以下是导致问题的代码段:
//This is the Entry point method of the Bot application
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
if (turnContext.Activity.Text == "@Bot GetRequests")
{
await HandleCommandAsync(turnContext);
}
}
public async virtual Task<bool> HandleCommandAsync(ITurnContext context)
{
Activity activity = context.Activity;
Command command = Command.FromMessageActivity(activity);
bool wasHandled = false;
Activity replyActivity = null;
ConversationReference sender = MessageRouter.CreateSenderConversationReference(activity);
switch (command.BaseCommand)
{
case Commands.GetRequests:
IList<ConnectionRequest> connectionRequests = _messageRouter.RoutingDataManager.GetConnectionRequests();
replyActivity = activity.CreateReply();
replyActivity.Attachments = CreateMultipleConnectionRequestCards(_messageRouter,
connectionRequests, activity.Recipient?.Name);
replyActivity.ChannelData = JsonConvert.SerializeObject(connectionRequests);
wasHandled = true;
break;
}
if (replyActivity != null)
{
//Exception is thrown here when the Bot is published to Teams channel'
await context.SendActivityAsync(replyActivity);
}
return wasHandled;
}
public static IList<Attachment> CreateMultipleConnectionRequestCards(MessageRouter messageRouter,
IList<ConnectionRequest> connectionRequests, string botName = null)
{
IList<Attachment> attachments = new List<Attachment>();
foreach (ConnectionRequest connectionRequest in connectionRequests)
{
attachments.Add(CreateConnectionRequestCard(messageRouter, connectionRequest, botName).ToAttachment());
}
return attachments;
}
我很想知道开发者社区中是否有人曾经遇到过类似的问题。如果是的话,如果能在此问题上得到任何帮助,我将不胜感激。
答案 0 :(得分:1)
我在团队中有同样的错误,下面是我的代码,在仿真器上除了团队外什么都没有显示,它使操作返回了无效的错误请求,
if (reasonintent == "Schedule")
{
//******************
UAT_LUIS_Entity UAT_LUIS = await GetEntityFromLUIS(turnContext.Activity.Text);
List<string> entityList = new List<string>();
String HourFromTime = "", HourToTime = "", MinToTime = "", MinFromTime = "", checkspecificdate = "";
String FromTime = "", ToTime = "", FromTimeHour = "", FromTimeMin = "", ToTimeHour = "", ToTimeMin = "";
for (int i = 0; i < UAT_LUIS.entities.Length; i++)
{
entityList.Add(UAT_LUIS.entities[i].type.ToString().ToLower());
if (UAT_LUIS.entities[i].type.ToString().ToLower() == "room")
{
roomluisname = UAT_LUIS.entities[i].entity.ToString().ToLower();
}
if (UAT_LUIS.entities[i].type.ToString().ToLower() == "date")
{
dateluis = UAT_LUIS.entities[i].entity.ToString().ToLower();
checkspecificdate = "true"
;
}
if (UAT_LUIS.entities[i].type.ToString().ToLower() == "today")
{
dateluis = UAT_LUIS.entities[i].entity.ToString().ToLower();
}
if (UAT_LUIS.entities[i].type.ToString().ToLower() == "the day after tomorrow")
{
dateluis = UAT_LUIS.entities[i].entity.ToString().ToLower();
}
if (UAT_LUIS.entities[i].type.ToString().ToLower() == "tomorrow")
{
dateluis = UAT_LUIS.entities[i].entity.ToString().ToLower();
}
}
String d = dateluis;
if (checkspecificdate == "true")
{
datevar = Convert.ToDateTime(dateluis);
}
else if (dateluis.Contains("today"))
{
datevar = DateTime.Today;
}
else if (dateluis.Contains( "tomorrow"))
{
datevar = DateTime.Today.AddDays(1);
}
else if (dateluis == "the day after tomorrow" || dateluis.Contains("day after tomorrow"))
{
datevar = DateTime.Today.AddDays(2);
}
else
{
datevar = DateTime.Today;
}
DateTime dateobject;
String datestring = "";
if (DateTime.TryParse(datevar.ToString(), out dateobject))
{
datestring = dateobject.ToString("yyyy-MM-ddT");
}
string datevarparticularstart = datestring + "00" + ":" + "00" + ":" + "01-08:00";
string datevarparticularend = datestring + "23" + ":" + "59" + ":" + "00-08:00";
var queryparticulardate = new List<QueryOption>()
{
new QueryOption("startDateTime", datevarparticularstart),
new QueryOption("endDateTime", datevarparticularend)
};
var calendarViewparticulardate = await graphClient.Me.CalendarView
.Request(queryparticulardate)
.GetAsync();
String luiscalenderentity = "";
//***********************
for (int i = 0; i < UAT_LUIS.entities.Length; i++)
{
entityList.Add(UAT_LUIS.entities[i].type.ToString().ToLower());
if (UAT_LUIS.entities[i].entity.ToString().ToLower().Contains("tulip"))
{
luiscalenderentity = "tulip";
}
else if (UAT_LUIS.entities[i].entity.ToString().ToLower().Contains("lotus"))
{
luiscalenderentity = "lotus";
}
else if (UAT_LUIS.entities[i].entity.ToString().ToLower().Contains("snowdrop"))
{
luiscalenderentity = "snowdrop";
}
else if (UAT_LUIS.entities[i].entity.ToString().ToLower().Contains("chanyaky"))
{
luiscalenderentity = "chanyaky";
}
}
string boolvarcheck = "";
if (calendarViewparticulardate.Count != 0)
{
for (int i = 0; i < calendarViewparticulardate.Count; i++)
{
if (calendarViewparticulardate[i].Location.DisplayName.ToLower().Contains(luiscalenderentity))
{
boolvarcheck = "true";
string converttostring1 = "";
DateTime date1;
if (DateTime.TryParse(Convert.ToDateTime(calendarViewparticulardate[i].Start.DateTime).AddHours(-8).ToString(), out date1))
{
converttostring1 = date1.ToString("HH:mm:ss");
}
String vartime1 = converttostring1;
// Convert.ToDateTime(calendarViewdate[0].End.DateTime).AddHours(-8);
string converttostring2 = "";
DateTime date2;
if (DateTime.TryParse(Convert.ToDateTime(calendarViewparticulardate[i].End.DateTime).AddHours(-8).ToString(), out date2))
{
converttostring2 = date2.ToString("HH:mm:ss");
}
String vartime2 = converttostring2;
EventNameschedule.Add(new AdaptiveTextBlock()
{
Text = calendarViewparticulardate[i].Location.DisplayName,
Size = AdaptiveTextSize.Small,
Color = AdaptiveTextColor.Accent
});
Eventschedulestart.Add(new AdaptiveTextBlock()
{
Text = vartime1,
Size = AdaptiveTextSize.Small,
Color = AdaptiveTextColor.Accent
});
Eventscheduleend.Add(new AdaptiveTextBlock()
{
Text = vartime2,
Size = AdaptiveTextSize.Small,
Color = AdaptiveTextColor.Accent
});
Eventschedulesubject.Add(new AdaptiveTextBlock()
{
Text = calendarViewparticulardate[i].Subject,
Size = AdaptiveTextSize.Small,
Color = AdaptiveTextColor.Accent
});
}
}
if (boolvarcheck == "true")
{
card.Body.Add(new AdaptiveContainer()
{
Items = new List<AdaptiveElement>()
{
new AdaptiveColumnSet()
{
Type = "ColumnSet",
Height = AdaptiveHeight.Auto,
Columns=new List<AdaptiveColumn> ()
{
new AdaptiveColumn()
{
Type="Column",
Width="200px",
Items=new List<AdaptiveElement>()
{
new AdaptiveTextBlock()
{
Type="TextBlock",
Text=luiscalenderentity,//+" Conference Hall",
Weight=AdaptiveTextWeight.Bolder,
Color=AdaptiveTextColor.Good
}
}
} ,
new AdaptiveColumn()
{
Type="Column",
Width="150px",
Items=new List<AdaptiveElement>()
{
new AdaptiveTextBlock()
{
Type="TextBlock",
Text=datevar.ToString("dd-MM-yyyy"),//DateTime.Today.ToString("dd-MM-yyyy"),
Weight=AdaptiveTextWeight.Bolder,
Color = AdaptiveTextColor.Good
}
}
},
}
},
new AdaptiveColumnSet()
{
Type = "ColumnSet",
Height = AdaptiveHeight.Auto,
Columns=new List<AdaptiveColumn> ()
{
new AdaptiveColumn()
{
Type="Column",
Width="200px",
Items=new List<AdaptiveElement>()
{
new AdaptiveTextBlock()
{
Type="TextBlock",
Text="Name",
Weight=AdaptiveTextWeight.Bolder,
Color=AdaptiveTextColor.Good
}
}
} ,
new AdaptiveColumn()
{
Type="Column",
Width="80px",
Items=new List<AdaptiveElement>()
{
new AdaptiveTextBlock()
{
Type="TextBlock",
Text="Start Time",
Weight=AdaptiveTextWeight.Bolder,
Color = AdaptiveTextColor.Good
}
}
},
new AdaptiveColumn()
{
Type="Column",
Width="80px",
Items=new List<AdaptiveElement>()
{
new AdaptiveTextBlock()
{
Type="TextBlock",
Text="End Time",
Weight=AdaptiveTextWeight.Bolder,
Color = AdaptiveTextColor.Good
}
}
},
}
},
new AdaptiveColumnSet()
{
Type = "ColumnSet",
Height = AdaptiveHeight.Auto,
Columns=new List<AdaptiveColumn> ()
{
new AdaptiveColumn()
{
Type="Column",
Width="200px",
Items=Eventschedulesubject,
},
new AdaptiveColumn()
{
Type="Column",
Width="80px",
Items= Eventschedulestart,
},
new AdaptiveColumn()
{
Type="Column",
Width="80px",
Items= Eventscheduleend,
}
}
},
}
});
}
else
{
card.Body.Add(new AdaptiveContainer()
{
Items = new List<AdaptiveElement>()
{
new AdaptiveColumnSet()
{
Type = "ColumnSet",
Height = AdaptiveHeight.Auto,
Columns=new List<AdaptiveColumn> ()
{
new AdaptiveColumn()
{
Type="Column",
Width="500px",
Items=new List<AdaptiveElement>()
{
new AdaptiveTextBlock()
{
Type="TextBlock",
Text=" No scheduling for "+luiscalenderentity+" hall",
Weight=AdaptiveTextWeight.Bolder,
Color=AdaptiveTextColor.Good
}
}
}
}
},
}
});
}
}
else
{
card.Body.Add(new AdaptiveContainer()
{
Items = new List<AdaptiveElement>()
{
new AdaptiveColumnSet()
{
Type = "ColumnSet",
Height = AdaptiveHeight.Auto,
Columns=new List<AdaptiveColumn> ()
{
new AdaptiveColumn()
{
Type="Column",
Width="500px",
Items=new List<AdaptiveElement>()
{
new AdaptiveTextBlock()
{
Type="TextBlock",
Text=" No scheduling for "+luiscalenderentity+" hall",
Weight=AdaptiveTextWeight.Bolder,
Color=AdaptiveTextColor.Good
}
}
}
}
},
}
});
}
var attachment1 = new Microsoft.Bot.Schema.Attachment
{
ContentType = AdaptiveCard.ContentType,
Content = card,
};
var reply1 = MessageFactory.Attachment(attachment1);
await turnContext.SendActivityAsync(reply1, cancellationToken);
}
答案 1 :(得分:0)
问题在HandleCommandAsync(ITurnContext context)方法的下面一行。
replyActivity.ChannelData = JsonConvert.SerializeObject(connectionRequests);
只需评论一下,它就可以工作。