如何为 Microsoft Web 机器人实现电子邮件服务
How do I implement the email service for microsoft web robot
我正在尝试将以下行添加到我的项目中,以测试 Microsoft Bot 框架的电子邮件功能:
List<ChannelAccount> participants = new List<ChannelAccount>();
participants.Add(new ChannelAccount("shansari@verizon.net", "Shahin Ansari"));
IMessageActivity message = Activity.CreateMessageActivity();
bool isGroup = false;
ConversationParameters cpMessage = new ConversationParameters(message.Recipient, isGroup, participants, "Quarter End Discussion");
var ConversationId = connector.Conversations.CreateConversationAsync(cpMessage);
//message.From = botChannelAccount;
message.From = new ChannelAccount();
message.Conversation = new ConversationAccount();
message.ChannelId = "email";
message.Text = "Hey, what's up everyone?";
message.Locale = "en-Us";
connector.Conversations.SendToConversation((Activity)message);
//await connector.Conversations.SendToConversation((Activity)message);
var replyMessage = activity.CreateReply("Yo, I heard you Sean.", "en");
connector.Conversations.ReplyToActivityAsync(replyMessage);
但是我没有收到电子邮件。
我知道发送 'Yo, I heard you Sean' 的 "print" 语句在我将它放在顶部时确实有效。但是当它在底部时它不会。所以在那几行中有些东西中断了。但是当我将它推送到 Azure 时,我不知道如何解决这个问题,并且在本地调试它不会给我太多帮助,因为它似乎编译得很好。有没有人这样做过?
我想我已经完成了您建议的所有更新,并已推送到 Azure,但没有生成电子邮件。这是我更新的代码:
namespace Bot_Application1
{
[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
// calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;
// return our reply to the user
Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
await connector.Conversations.ReplyToActivityAsync(reply);
var replyMessage = activity.CreateReply("Yo, I heard you Sean.", "en");
await connector.Conversations.ReplyToActivityAsync(replyMessage);
// Start of my own code
//try {
// The code until the line above
List<ChannelAccount> participants = new List<ChannelAccount>();
participants.Add(new ChannelAccount("shansari@verizon.net", "Shahin Ansari"));
IMessageActivity message = Activity.CreateMessageActivity();
bool isGroup = false;
ConversationParameters cpMessage = new ConversationParameters(message.Recipient, isGroup, participants, "Quarter End Discussion");
var ConversationId = await connector.Conversations.CreateConversationAsync(cpMessage);
message.Recipient = new ChannelAccount("shansari@verizon.net", "Shahin Ansari");
//message.From = new ChannelAccount("shansari@verizon.net", "Shahin Ansari");
message.From = new ChannelAccount();
//message.From = message.ChannelData;
message.Conversation = new ConversationAccount(id: ConversationId.Id);
message.ChannelId = "email";
message.Text = "Hey, what's up everyone?";
message.Locale = "en-Us";
connector.Conversations.SendToConversation((Activity)message);
//await connector.Conversations.SendToConversation((Activity)message);
//}
//catch (Exception ex)
//{
// Trace.TraceError("Sean's Exception: " + ex.ToString());
// throw;
//}
//await connector.Conversations.ReplyToActivityAsync(reply);
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing tha the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}
return null;
}
}
}
主要问题是您需要在消息中设置对话值。也应该等待异步函数:
List<ChannelAccount> participants = new List<ChannelAccount>();
participants.Add(new ChannelAccount("shansari@verizon.net", "Shahin Ansari"));
IMessageActivity message = Activity.CreateMessageActivity();
bool isGroup = false;
ConversationParameters cpMessage = new ConversationParameters(message.Recipient, isGroup, participants, "Quarter End Discussion");
var ConversationId = await connector.Conversations.CreateConversationAsync(cpMessage);
//message.From = botChannelAccount;
message.From = new ChannelAccount();
message.Conversation = new ConversationAccount();
message.ChannelId = "email";
message.Conversation = new ConversationAccount(id: ConversationId.Id);
message.Text = "Hey, what's up everyone?";
message.Locale = "en-Us";
connector.Conversations.SendToConversation((Activity)message);
//await connector.Conversations.SendToConversation((Activity)message);
var replyMessage = activity.CreateReply("Yo, I heard you Sean.", "en");
await connector.Conversations.ReplyToActivityAsync(replyMessage);
我正在尝试将以下行添加到我的项目中,以测试 Microsoft Bot 框架的电子邮件功能:
List<ChannelAccount> participants = new List<ChannelAccount>();
participants.Add(new ChannelAccount("shansari@verizon.net", "Shahin Ansari"));
IMessageActivity message = Activity.CreateMessageActivity();
bool isGroup = false;
ConversationParameters cpMessage = new ConversationParameters(message.Recipient, isGroup, participants, "Quarter End Discussion");
var ConversationId = connector.Conversations.CreateConversationAsync(cpMessage);
//message.From = botChannelAccount;
message.From = new ChannelAccount();
message.Conversation = new ConversationAccount();
message.ChannelId = "email";
message.Text = "Hey, what's up everyone?";
message.Locale = "en-Us";
connector.Conversations.SendToConversation((Activity)message);
//await connector.Conversations.SendToConversation((Activity)message);
var replyMessage = activity.CreateReply("Yo, I heard you Sean.", "en");
connector.Conversations.ReplyToActivityAsync(replyMessage);
但是我没有收到电子邮件。
我知道发送 'Yo, I heard you Sean' 的 "print" 语句在我将它放在顶部时确实有效。但是当它在底部时它不会。所以在那几行中有些东西中断了。但是当我将它推送到 Azure 时,我不知道如何解决这个问题,并且在本地调试它不会给我太多帮助,因为它似乎编译得很好。有没有人这样做过?
我想我已经完成了您建议的所有更新,并已推送到 Azure,但没有生成电子邮件。这是我更新的代码:
namespace Bot_Application1
{
[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
// calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;
// return our reply to the user
Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
await connector.Conversations.ReplyToActivityAsync(reply);
var replyMessage = activity.CreateReply("Yo, I heard you Sean.", "en");
await connector.Conversations.ReplyToActivityAsync(replyMessage);
// Start of my own code
//try {
// The code until the line above
List<ChannelAccount> participants = new List<ChannelAccount>();
participants.Add(new ChannelAccount("shansari@verizon.net", "Shahin Ansari"));
IMessageActivity message = Activity.CreateMessageActivity();
bool isGroup = false;
ConversationParameters cpMessage = new ConversationParameters(message.Recipient, isGroup, participants, "Quarter End Discussion");
var ConversationId = await connector.Conversations.CreateConversationAsync(cpMessage);
message.Recipient = new ChannelAccount("shansari@verizon.net", "Shahin Ansari");
//message.From = new ChannelAccount("shansari@verizon.net", "Shahin Ansari");
message.From = new ChannelAccount();
//message.From = message.ChannelData;
message.Conversation = new ConversationAccount(id: ConversationId.Id);
message.ChannelId = "email";
message.Text = "Hey, what's up everyone?";
message.Locale = "en-Us";
connector.Conversations.SendToConversation((Activity)message);
//await connector.Conversations.SendToConversation((Activity)message);
//}
//catch (Exception ex)
//{
// Trace.TraceError("Sean's Exception: " + ex.ToString());
// throw;
//}
//await connector.Conversations.ReplyToActivityAsync(reply);
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing tha the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}
return null;
}
}
}
主要问题是您需要在消息中设置对话值。也应该等待异步函数:
List<ChannelAccount> participants = new List<ChannelAccount>();
participants.Add(new ChannelAccount("shansari@verizon.net", "Shahin Ansari"));
IMessageActivity message = Activity.CreateMessageActivity();
bool isGroup = false;
ConversationParameters cpMessage = new ConversationParameters(message.Recipient, isGroup, participants, "Quarter End Discussion");
var ConversationId = await connector.Conversations.CreateConversationAsync(cpMessage);
//message.From = botChannelAccount;
message.From = new ChannelAccount();
message.Conversation = new ConversationAccount();
message.ChannelId = "email";
message.Conversation = new ConversationAccount(id: ConversationId.Id);
message.Text = "Hey, what's up everyone?";
message.Locale = "en-Us";
connector.Conversations.SendToConversation((Activity)message);
//await connector.Conversations.SendToConversation((Activity)message);
var replyMessage = activity.CreateReply("Yo, I heard you Sean.", "en");
await connector.Conversations.ReplyToActivityAsync(replyMessage);