/// <summary> /// Retrieves the list of users in the given spreadsheet ID. /// </summary> private bool GetSpreadsheetList() { string json = string.Empty; try { json = new WebClient().DownloadString("https://spreadsheets.google.com/feeds/list/" + Properties.Settings.Default.spreadSheetID + "/od6/public/values?alt=json"); } catch { ShowMessageBoxError("Error!", "Error on retrieving data from the spreadsheet.\nPlease, check the spreadsheet ID and try again."); } if (string.IsNullOrEmpty(json)) { return(false); } dynamic jsonObj = JsonConvert.DeserializeObject(json); if (jsonObj.feed.entry != null) { foreach (var entry in jsonObj.feed.entry) { StreamersListNames.Add(entry["title"]["$t"].ToString().Trim().ToLower()); } if (StreamersListNames.Count <= 0) { ShowMessageBoxError("Error!", "No entries found in the spreadsheet."); return(false); } } else { ShowMessageBoxError("Error!", "Error on retrieving data from the spreadsheet."); return(false); } return(true); }
private void ConnectDisconnectButton_Click(object sender, EventArgs e) { StreamersListNames.Clear(); BannedChannels.Clear(); if (!BotConnected) { List <string> errorMessages = new List <string>(); if (string.IsNullOrWhiteSpace(channelTextBox.Text) || channelTextBox.Text == BotSettings.ChannelTextBoxPlaceholder) { errorMessages.Add("- No channel name set."); channelTextBox.BackColor = Color.Yellow; } if (string.IsNullOrWhiteSpace(twithcUserTextBox.Text) || twithcUserTextBox.Text == BotSettings.TwithcUserTextBoxPlaceholder) { errorMessages.Add("- No username set."); twithcUserTextBox.BackColor = Color.Yellow; } if (string.IsNullOrWhiteSpace(oAuthTextBox.Text) || oAuthTextBox.Text == BotSettings.OAuthTextBoxPlaceholder) { errorMessages.Add("- No OAuth token set."); oAuthTextBox.BackColor = Color.Yellow; } if (((sayHelloCheckBox.Checked && sayHelloRadioButton2.Checked) || promoteChannelsCheckBox.Checked) && (string.IsNullOrWhiteSpace(spreadsheetIdTextBox.Text) || spreadsheetIdTextBox.Text == BotSettings.SpreadsheetIdPlaceholder)) { errorMessages.Add("- No spreadsheet ID set."); spreadsheetIdTextBox.BackColor = Color.Yellow; } if (errorMessages.Count > 0) { string error = string.Empty; foreach (string msg in errorMessages) { error += msg + "\n"; } error += "\nPlease, fix it and try again."; ChangeConnectionStatusLabel("Error!", true); ShowMessageBoxError("Configuration Error", error); } else { Properties.Settings.Default.channelName = channelTextBox.Text.Trim(); Properties.Settings.Default.userName = twithcUserTextBox.Text.Trim(); Properties.Settings.Default.oAuthToken = oAuthTextBox.Text.Trim(); Properties.Settings.Default.spreadSheetID = spreadsheetIdTextBox.Text.Trim(); Properties.Settings.Default.bannedChannels = bannedChannelsTextBox.Text.Trim(); Properties.Settings.Default.Save(); if (!string.IsNullOrWhiteSpace(bannedChannelsTextBox.Text) && bannedChannelsTextBox.Text != BotSettings.BannedChannelsPlaceholder) { if (Properties.Settings.Default.bannedChannels.Contains(',')) { string[] bannedUsers = Properties.Settings.Default.bannedChannels.ToLower().Split(','); foreach (string user in bannedUsers) { if (!string.IsNullOrWhiteSpace(user)) { BannedChannels.Add(user.Trim()); } } } else { BannedChannels.Add(Properties.Settings.Default.bannedChannels.ToLower()); } } if (string.IsNullOrWhiteSpace(spreadsheetIdTextBox.Text) || spreadsheetIdTextBox.Text == BotSettings.SpreadsheetIdPlaceholder) { promoteChannelsCheckBox.Enabled = false; sayHelloRadioButton2.Enabled = false; } if (Properties.Settings.Default.spreadSheetID.Contains(',')) { string[] users = Properties.Settings.Default.spreadSheetID.ToLower().Split(','); foreach (string user in users) { if (!string.IsNullOrWhiteSpace(user)) { StreamersListNames.Add(user.Trim()); } } if (StreamersListNames.Count <= 0) { ShowMessageBoxError("Error!", "No names found."); } else { ConnectChatBot(); } } else if (Properties.Settings.Default.spreadSheetID.Length <= 25) { StreamersListNames.Add(Properties.Settings.Default.spreadSheetID.ToLower()); ConnectChatBot(); } else { if (GetSpreadsheetList()) { ConnectChatBot(); } } } } else { promoteChannelsCheckBox.Enabled = true; sayHelloRadioButton2.Enabled = true; connectionStatusLabel.Text = "Desconnectant..."; chatBot.DisconnectBot(); } }