Sending the status to citizen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// The bot is obviously stateless. When we get the message from D365, we need to parse the data to find out which citizen should receive what message on which channel. | |
/// </summary> | |
/// <param name="activity"></param> | |
private void CommunicateMessageToUser(Activity activity) | |
{ | |
var message = JsonConvert.DeserializeObject<QueueMessage>(((JObject)activity.Value).GetValue("Message").ToString()); | |
//Conversation ID is generally the key in order to reply to the same conversation in the channel. | |
if(!string.IsNullOrEmpty(message.ConversationID)) | |
{ | |
var recipient = new ChannelAccount(message.RecipientID, message.RecipientName); | |
var from = new ChannelAccount(message.FromID, message.FromName); | |
//These couple of lines are important to estabilish the trust that we are authorized to send the message in this channel. | |
var connector = new ConnectorClient(new Uri(message.ServiceURL), new MicrosoftAppCredentials(ApiKey, ApiEndpoint)); | |
MicrosoftAppCredentials.TrustServiceUrl(message.ServiceURL); | |
var alertMessage = Activity.CreateMessageActivity(); | |
if (!string.IsNullOrEmpty(message.ConversationID) && !string.IsNullOrEmpty(message.ChannelID)) | |
{ | |
alertMessage.ChannelId = message.ChannelID; | |
} | |
else | |
{ | |
message.ConversationID = (connector.Conversations.CreateDirectConversationAsync(from, recipient)).Id.ToString(); | |
} | |
//I know I am flipping the "recipient" and "from" objects. The reason is that when we originally received the message from the citizen, | |
//the citizen related attributes is present in the from object and bot related attributes are present in the recipient object and it makes sense as, | |
//the citizen sends the message and the bot receives it. But, now the table has turned. The Bot sends the message and the citizen receives it, | |
// which is why i flip it. | |
alertMessage.Recipient = from; | |
alertMessage.From = recipient; | |
alertMessage.Conversation = new ConversationAccount(id: message.ConversationID); | |
alertMessage.Text = message.Alert; | |
alertMessage.Locale = "en-Us"; | |
//Once the connection is established to the channel and the conversation, the bot can reply with the text. | |
connector.Conversations.SendToConversationAsync((Activity)alertMessage); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment