Skip to content

Instantly share code, notes, and snippets.

@coderbec
Created January 23, 2019 04:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save coderbec/25890d39a3d4a6f195b49c4f52eb40db to your computer and use it in GitHub Desktop.
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 TimeSheetReminderApp
{
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 = "TimeSheetReminder";
static void Main(string[] args)
{
//read the Google API credentials
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;
//Create the MessageMedia client
MessageMediaMessagesClient client = new MessageMediaMessagesClient();
MessagesController messages = client.Messages;
// set up the message request body
SendMessagesRequest body = new SendMessagesRequest();
body.Messages = new List<Message>();
if (values != null && values.Count > 0)
{
Console.WriteLine("Name, Filled");
foreach (var row in values)
{
string name = row[1].ToString();
string number = row[2].ToString();
string filled = row[5].ToString();
Console.WriteLine("{0}, {1}", row[1], row[5]);
if (filled == "No")
{
Message body_messages_0 = new Message();
body_messages_0.Content = $"Hi {name}, \n Please make sure you fill in your timesheets.";
body_messages_0.DestinationNumber = number;
body.Messages.Add(body_messages_0);
}
}
}
else
{
Console.WriteLine("No data found.");
}
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