private async Task <bool> isNewUser(string userId, Microsoft.Bot.Connector.Activity message, TableBotDataStore botStateStore) { IBotDataStore <BotData> dataStore = botStateStore; pigLatinBotUserData addedUserData = new pigLatinBotUserData(); BotData botData = new BotData(); try { botData = (BotData)await dataStore.LoadAsync(new Address(message.Recipient.Id, message.ChannelId, userId, message.Conversation.Id, message.ServiceUrl), BotStoreType.BotUserData, default(CancellationToken)); } catch (Exception e) { if (e.Message == "Resource not found") { } else { throw e; } } if (botData.Data == null) { botData = new BotData(eTag: "*"); } addedUserData = botData.GetProperty <pigLatinBotUserData>("v1") ?? new pigLatinBotUserData(); if (addedUserData.isNewUser == true) { return(true); } else { return(false); } }
private async Task <Microsoft.Bot.Connector.Activity> dataTypesTest(Microsoft.Bot.Connector.Activity message, ConnectorClient connector, TableBotDataStore botStateStore) { IBotDataStore <BotData> dataStore = botStateStore; pigLatinBotUserData deleteUserData = new pigLatinBotUserData(); StringBuilder sb = new StringBuilder(); // DM a user DateTime timestamp = DateTime.UtcNow; pigLatinBotUserData addedUserData = new pigLatinBotUserData(); BotData botData = new BotData(); try { botData = (BotData)await dataStore.LoadAsync(new Address(message.Recipient.Id, message.ChannelId, message.From.Id, message.Conversation.Id, message.ServiceUrl), BotStoreType.BotUserData, default(CancellationToken)); } catch (Exception e) { if (e.Message == "Resource not found") { } else { throw e; } } if (botData == null) { botData = new BotData(eTag: "*"); } addedUserData = botData.GetProperty <pigLatinBotUserData>("v1") ?? new pigLatinBotUserData(); addedUserData.isNewUser = false; addedUserData.lastReadLegalese = timestamp; try { botData.SetProperty("v1", addedUserData); await dataStore.SaveAsync(new Address(message.Recipient.Id, message.ChannelId, message.From.Id, message.Conversation.Id, message.ServiceUrl), BotStoreType.BotUserData, botData, default(CancellationToken)); } catch (Exception e) { Trace.WriteLine(e.Message); } try { botData = (BotData)await dataStore.LoadAsync(new Address(message.Recipient.Id, message.ChannelId, message.From.Id, message.Conversation.Id, message.ServiceUrl), BotStoreType.BotUserData, default(CancellationToken)); } catch (Exception e) { if (e.Message == "Resource not found") { } else { throw e; } } if (botData == null) { botData = new BotData(eTag: "*"); } addedUserData = botData.GetProperty <pigLatinBotUserData>("v1") ?? new pigLatinBotUserData(); if (addedUserData.isNewUser != false || addedUserData.lastReadLegalese != timestamp) { sb.Append(translateToPigLatin("Bot data didn't match doofus.")); } else { sb.Append(translateToPigLatin("Yo, that get/save/get thing worked.")); } return(message.CreateReply(sb.ToString(), "en-Us")); }
private async Task <Microsoft.Bot.Connector.Activity> handleSystemMessagesAsync(Microsoft.Bot.Connector.Activity message, ConnectorClient connector, TableBotDataStore botStateStore) { Microsoft.Bot.Connector.Activity replyMessage = message.CreateReply(); message.Locale = "en"; IBotDataStore <BotData> dataStore = botStateStore; switch (message.GetActivityType()) { case ActivityTypes.DeleteUserData: //In this case the DeleteUserData message comes from the user so we can clear the data and set it back directly BotData currentBotData = (BotData)await dataStore.LoadAsync(new Address(message.Recipient.Id, message.ChannelId, message.From.Id, message.Conversation.Id, message.ServiceUrl), BotStoreType.BotUserData, default(CancellationToken)); pigLatinBotUserData deleteUserData = new pigLatinBotUserData(); currentBotData.SetProperty("v1", deleteUserData); await dataStore.SaveAsync(new Address(message.Recipient.Id, message.ChannelId, message.From.Id, message.Conversation.Id, message.ServiceUrl), BotStoreType.BotUserData, currentBotData, default(CancellationToken)); replyMessage.Text = translateToPigLatin("I have deleted your data oh masterful one"); Trace.TraceInformation("Clearing user's BotUserData"); return(replyMessage); //if they're new or haven't seen the updated legal documents, send them a message //use the incoming message to set up the outgoing message case ActivityTypes.ConversationUpdate: foreach (ChannelAccount added in message.MembersAdded) { Microsoft.Bot.Connector.Activity addedMessage = message.CreateReply(); bool needToSendWelcomeText = false; pigLatinBotUserData addedUserData = new pigLatinBotUserData(); BotData botData = new BotData(); // is the added member me? if (added.Id == message.Recipient.Id) { addedMessage.Text = string.Format(translateToPigLatin("Hey there, I'm PigLatinBot. I make intelligible text unintelligible. Ask me how by typing 'Help', and for terms and info, click ") + "[erehay](http://www.piglatinbot.com)", added.Name); var reply = await connector.Conversations.ReplyToActivityAsync(addedMessage); continue; } if (await isNewUser(added.Id, message, botStateStore)) { addedUserData.isNewUser = false; needToSendWelcomeText = true; } if (addedUserData.lastReadLegalese < lastModifiedPolicies) { addedUserData.lastReadLegalese = DateTime.UtcNow; needToSendWelcomeText = true; } if (needToSendWelcomeText) { addedMessage.Text = string.Format(translateToPigLatin("Welcome to the chat") + " {0}, " + translateToPigLatin("I'm PigLatinBot. I make intelligible text unintelligible. Ask me how by typing 'Help', and for terms and info, click ") + "[erehay](http://www.piglatinbot.com)", added.Name); addedMessage.Recipient = added; addedMessage.Conversation = null; try { botData.SetProperty("v1", addedUserData); await dataStore.SaveAsync(new Address(message.Recipient.Id, message.ChannelId, added.Id, message.Conversation.Id, message.ServiceUrl), BotStoreType.BotUserData, botData, default(CancellationToken)); } catch (Exception e) { Trace.WriteLine(e.Message); } var ConversationId = await connector.Conversations.CreateDirectConversationAsync(message.Recipient, message.From); addedMessage.Conversation = new ConversationAccount(id: ConversationId.Id); var reply = await connector.Conversations.SendToConversationAsync(addedMessage); } } //maybe someone got removed foreach (ChannelAccount removed in message.MembersRemoved) { Microsoft.Bot.Connector.Activity removedMessage = message.CreateReply(); removedMessage.Locale = "en"; removedMessage.Text = string.Format("{0}", removed.Name) + translateToPigLatin(" has Left the building"); var reply = await connector.Conversations.ReplyToActivityAsync(removedMessage); } return(null); default: return(null); } }