我从我的.NET机器人上打开了一个网页。该页面从我们的一个后端系统与用户交互。一旦交互结束,我需要向机器人发送状态更新消息-它位于context.Wait中,同时等待该消息。
目前,机器人正在使用Facebook频道,并通过Facebook Url按钮启动页面,但最终它将需要跨多个频道工作。
从网站上,我可以很容易地向用户发送消息,但尽管我花了几个小时搜索和尝试不同的机制,我还没有找到一种方法来发送消息给机器人。
基于https://docs.botframework.com/en-us/csharp/builder/sdkreference/d1/df2/_conversation_reference_ex_8cs_source.html的最新尝试,(cr已缓存会话详情):
string MicrosoftAppId = ConfigurationManager.AppSettings["MicrosoftAppId"];
string MicrosoftAppPassword = ConfigurationManager.AppSettings["MicrosoftAppPassword"];
var account = new MicrosoftAppCredentials(MicrosoftAppId, MicrosoftAppPassword);
MicrosoftAppCredentials.TrustServiceUrl(cr.serviceUrl);
var connector = new ConnectorClient(new Uri(cr.serviceUrl), account);
Activity activity = new Activity
{
Type = ActivityTypes.Message,
Id = Guid.NewGuid().ToString(),
Recipient = new ChannelAccount
{
Id = cr.bot.id,
Name = cr.bot.name
},
ChannelId = cr.channelId,
ServiceUrl = cr.serviceUrl,
Conversation = new ConversationAccount
{
Id = cr.conversation.id,
IsGroup = false,
Name = null
},
From = new ChannelAccount
{
Id = cr.bot.id,
Name = cr.bot.name
},
Text = "Test send message to bot from web service"
};
try
{
await connector.Conversations.SendToConversationAsync(activity);
}
catch (Exception ex)
{
var s = ex.Message;
}但似乎不会将发件人/收件人的组合发送给机器人。
我肯定我错过了一些简单的东西,你们可以告诉我它是什么!
发布于 2018-06-27 05:34:41
下面是一个从另一个应用程序向机器人发送消息的示例。在本例中,我是通过一个web API执行此操作的,该API是一个代理,用于拦截来自用户的消息并将其发送给机器人。这段代码中没有包括如何构造活动,但看起来您已经对该部分进行了排序。请注意,在这个辅助应用程序中,我使用的是Bot.Builder,因此我可以使用activity对象和其他特性。
//get a token (See below)
var token = GetToken();
//set the service url where you want this activity to be replied to
activity.ServiceUrl = "http://localhost:4643/api/return";
//convert an activity to json to send to bot
var jsonActivityAltered = JsonConvert.SerializeObject(activity);
//send a Web Request to the bot
using (var client = new WebClient())
{
//add your headers
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("Authorization", $"Bearer {token}");
try
{
//set where to to send the request {Your Bots Endpoint}
var btmResponse = client.UploadString("http://localhost:3971/api/messages", jsonActivityAltered);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}要获取令牌,请执行以下操作:
private static string GetToken()
{
string token;
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["grant_type"] = "client_credentials";
values["client_id"] = "{MS APP ID}";
values["client_secret"] = "{MS APP SECRET}";
values["scope"] = "{MS APP ID}/.default";
var response =
client.UploadValues("https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token", values);
var responseString = Encoding.Default.GetString(response);
var result = JsonConvert.DeserializeObject<ResponseObject>(responseString);
token = result.access_token;
}
return token;
}响应对象类:
public class ResponseObject
{
public string token_type { get; set; }
public int expires_in { get; set; }
public int ext_expires_in { get; set; }
public string access_token { get; set; }
}https://stackoverflow.com/questions/51040452
复制相似问题