Last active
July 4, 2018 12:59
-
-
Save senthilsweb/381308e50d400ae62311 to your computer and use it in GitHub Desktop.
C# class file to send SMS by adding Google Calender Event who in turn sends SMS notifications & reminder provided the user enabled SMS alert in his/her google calendar settings.
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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Security.Cryptography.X509Certificates; | |
using Google.Apis.Auth.OAuth2; | |
using Google.Apis.Calendar.v3; | |
using Google.Apis.Services; | |
using Google.Apis.Calendar.v3.Data; | |
using System.Dynamic; | |
/// <summary> | |
/// C# class file to send SMS by adding Google Calender Event who in turn sends SMS notifications & reminder provided the user enabled SMS alert in his/her google calendar settings. | |
/// </summary> | |
public class GoogleCalendarEventSmsNotifier | |
{ | |
String _keyfile; | |
String _keyPassword; | |
String _serviceAccountEmail; | |
String _calendarId; | |
public GoogleCalendarEventSmsNotifier(String keyFile, String keyPassword, String serviceAccountEmail, String calendarId) | |
{ | |
_keyfile = keyFile; | |
_keyPassword = keyPassword; | |
_serviceAccountEmail = serviceAccountEmail; | |
_calendarId = calendarId; | |
} | |
/// <summary> | |
/// Creates Google Calendar Event with reminder and notification enabled. | |
/// </summary> | |
/// <example> | |
/// dynamic request = new ExpandoObject(); | |
/// request.timeZone = "Asia/Calcutta"; | |
/// request.eventStart = Convert.ToDateTime(DateTime.UtcNow.AddSeconds(80).ToString("yyyy-MM-dd'T'HH:mm:ssZ")); | |
/// request.eventEnd = Convert.ToDateTime(DateTime.UtcNow.AddSeconds(80).AddMinutes(15).ToString("yyyy-MM-dd'T'HH:mm:ssZ")); | |
/// request.eventReminderMinutes = 1; | |
/// request.eventSummary = "Approval Request"; | |
/// request.eventDescription = "Samy has sent a approval for expense claim"; | |
/// request.eventOrganizerName = "VM Workflow"; | |
/// request.eventOrganizerEmail = "mobility@organizeremail.com"; | |
/// request.eventCreatorName = "VM Workflow"; | |
/// request.eventCreatorEmail = "mobility@creatoremail.com"; | |
/// request.eventAttendeeName = "Johny Michael"; | |
/// request.eventAttendeeEmail = "attendee@youremail.com"; | |
/// request.eventLocation = "My Apps"; | |
/// | |
/// GoogleCalendarEventSmsNotifier smsNotifier = new GoogleCalendarEventSmsNotifier(Server.MapPath(@"/key.p12"), "your-password", "youraccount@developer.gserviceaccount.com","primary"); | |
/// dynamic response = smsNotifier.SendSms(request); | |
/// | |
/// </example> | |
/// <param name="request">Dynamic request object</param> | |
/// <returns>Dynamic response object</returns> | |
public dynamic SendSms(dynamic request) | |
{ | |
dynamic response = new ExpandoObject(); | |
response.Status = true; | |
var certificate = new X509Certificate2(_keyfile, _keyPassword, X509KeyStorageFlags.Exportable); | |
ServiceAccountCredential credential = new ServiceAccountCredential( | |
new ServiceAccountCredential.Initializer(_serviceAccountEmail) | |
{ | |
Scopes = new[] { CalendarService.Scope.Calendar } | |
}.FromCertificate(certificate)); | |
// Create the service. | |
var service = new CalendarService(new BaseClientService.Initializer() | |
{ | |
HttpClientInitializer = credential, | |
ApplicationName = "Calendar Api", | |
ApiKey = "" | |
}); | |
Google.Apis.Calendar.v3.Data.Event newEvent = new Google.Apis.Calendar.v3.Data.Event(); | |
newEvent.Kind = "calendar#event"; | |
EventDateTime EStart = new EventDateTime(); | |
EStart.DateTime = request.eventStart; | |
newEvent.Start = EStart; | |
EventDateTime EEnd = new EventDateTime(); | |
EEnd.DateTime = request.eventEnd; | |
newEvent.End = EEnd; | |
newEvent.Reminders = new Event.RemindersData(); | |
newEvent.Reminders.UseDefault = false; | |
EventReminder ER = new EventReminder(); | |
ER.Method = "sms"; | |
ER.Minutes = request.eventReminderMinutes; | |
newEvent.Reminders.Overrides = new List<EventReminder>(); | |
newEvent.Reminders.Overrides.Add(ER); | |
newEvent.Summary = request.eventSummary; | |
newEvent.Description = request.eventDescription; | |
newEvent.Organizer = new Event.OrganizerData() { DisplayName = request.eventOrganizerName, Email = request.eventOrganizerEmail, Self = true }; | |
newEvent.Creator = new Event.CreatorData() { DisplayName = request.eventCreatorName, Email = request.eventCreatorEmail, Self = true }; | |
EventAttendee attendee = new EventAttendee(); | |
attendee.Email = request.eventAttendeeEmail; | |
attendee.DisplayName = request.eventAttendeeName; | |
newEvent.Location = request.eventLocation; | |
IList<EventAttendee> myAttendees = new List<EventAttendee>(); | |
myAttendees.Add(attendee); | |
newEvent.Attendees = myAttendees; | |
try | |
{ | |
//service.Calendars.Insert(newCal).Execute(); | |
EventsResource.InsertRequest insertRequest = service.Events.Insert(newEvent, _calendarId); | |
insertRequest.SendNotifications = true; | |
response.Result = insertRequest.Execute(); | |
} | |
catch (Exception e) | |
{ | |
response.Status = false; | |
response.Exception = e; | |
response.Message = e.Message; | |
} | |
response.Message = "SMS has been sent successfully"; | |
return response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment