Last active
January 23, 2019 04:23
Star
You must be signed in to star a gist
Implementation of the MessageMedia C# SDK that pulls data from Google Sheets to send reminders
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
using Google.Apis.Auth.OAuth2; | |
using Google.Apis.Sheets.v4; | |
using Google.Apis.Sheets.v4.Data; | |
using Google.Apis.Services; | |
using Google.Apis.Util.Store; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Threading; | |
using System; | |
using MessageMedia.Messages; | |
using MessageMedia.Messages.Models; | |
using MessageMedia.Messages.Exceptions; | |
using MessageMedia.Messages.Controllers; | |
namespace ShiftReminderApp | |
{ | |
class Program | |
{ | |
// If modifying these scopes, delete your previously saved credentials | |
// at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json | |
static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly }; | |
static string ApplicationName = "ShiftReminderApp"; | |
static void Main(string[] args) | |
{ | |
MessageMediaMessagesClient client = new MessageMediaMessagesClient(); | |
MessagesController messages = client.Messages; | |
UserCredential credential; | |
using (var stream = | |
new FileStream("credentials.json", FileMode.Open, FileAccess.Read)) | |
{ | |
// The file token.json stores the user's access and refresh tokens, and is created | |
// automatically when the authorization flow completes for the first time. | |
string credPath = "token.json"; | |
credential = GoogleWebAuthorizationBroker.AuthorizeAsync( | |
GoogleClientSecrets.Load(stream).Secrets, | |
Scopes, | |
"user", | |
CancellationToken.None, | |
new FileDataStore(credPath, true)).Result; | |
Console.WriteLine("Credential file saved to: " + credPath); | |
} | |
// Create Google Sheets API service. | |
var service = new SheetsService(new BaseClientService.Initializer() | |
{ | |
HttpClientInitializer = credential, | |
ApplicationName = ApplicationName, | |
}); | |
// Define request parameters. | |
String spreadsheetId = "1tUEdWwA-cGjVUD_0O2Rk_AFQcN-Sv9w4p7y72g326Rw"; | |
String range = "Reminders!A2:E"; | |
SpreadsheetsResource.ValuesResource.GetRequest request = | |
service.Spreadsheets.Values.Get(spreadsheetId, range); | |
// Prints the names and majors of students in a sample spreadsheet: | |
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit | |
ValueRange response = request.Execute(); | |
IList<IList<Object>> values = response.Values; | |
// set up the message request body | |
SendMessagesRequest body = new SendMessagesRequest(); | |
body.Messages = new List<Message>(); | |
if (values != null && values.Count > 0) | |
{ | |
Console.WriteLine("Number, Time"); | |
foreach (var row in values) | |
{ | |
string name = row[1].ToString(); | |
string time = row[3].ToString(); | |
string number = row[2].ToString(); | |
string location = row[4].ToString(); | |
Console.WriteLine("{0}, {1}", row[2], row[3]); | |
Message body_messages_0 = new Message(); | |
body_messages_0.Content = $"Hi {name}, \n Your next shift is at {time} - {location}"; | |
body_messages_0.DestinationNumber = number; | |
body.Messages.Add(body_messages_0); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("No data found."); | |
} | |
//Console.Read(); | |
try | |
{ | |
SendMessagesResponse result = messages.SendMessagesAsync(body).Result; | |
Console.WriteLine(result); | |
} | |
catch (APIException e) | |
{ | |
Console.WriteLine(e.Message + e.ResponseCode + e.HttpContext.ToString()); | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment