Click here to Skip to main content
Click here to Skip to main content

IVR used in Google Calendar Event

, 20 Aug 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
When the event in Google Calendar raises up,call user in IVR

Introduction for the IVR and Google Calendar

Many people have the experience of using the Calendar,it does facilitate our works and lifes.But most of the Calendars are based on text alert, that means you can only get the reminder event in front of your computer.

So I think if the Calendar can remind me by phone.If I want to achieve this idea, I need a Calendar that can provide me APIs, a technology that can make me to call through programming.

Fortunately,these technologies for this idea have been provided ,Google Calendar and IVR(Interactive Voice Response, based on phone call).

I've posted some articles to introduce IVR.You can view them here:

IVR used in Voicent Gateway
IVR used in Facebook

Or, you can investigate it throught the Google Search,there are lots of article to introduce it.Now, we'll mainly concentrate on the Google Calendar.

Google Calendar allows client applications to view and update calendar events in the form of Google data API ("GData") feeds. Your client application can use the Google Calendar data API to create new events, edit or delete existing events, and query for events that match particular criteria.

There are many possible uses for the Calendar data API. For example, you can create a web front end for your group's calendar that uses Google Calendar as a back end. Or you can generate a public calendar for Google Calendar to display, based on your organization's event database. Or you can search relevant calendars to display a list of upcoming events on those calendars.

You can view these articles to comprehend the Google Calendar:

What are the Google Data APIs?

Google Data APIs Client Libraries

Google Data APIs Samples

Developer's Guide

These articles provide detailed introductions and examples for using the .NET Client Library to work with the Google Calendar service. For help setting up the client library, see the Getting Started Guide. You will find examples of adding events, updating events, deleting events and querying events. If you're interested in understanding more about the underlying protocol used by the .NET Client Library to interact with the Calendar data API, please see the protocol tab.

After you read those,you can continue the following topics.

How to create Calendar Service

The CalendarService class represents a client connection (with authentication) to a Calendar service. To request a feed using the .NET client library, first instantiate a new CalendarService object and authenticate the user. Then use the Query method to retrieve a CalendarFeed object that contains entries for all of the user's calendars.

You can authenticate the user by WebForm or WinForm,the WebForm is called
AuthSub proxy authentication, and the WinForm is called ClientLogin username/password authentication.

In order to authenticate the applications using the AuthSub login protocol,you must get the Session token.

First,construct a AuthSubRequest URL for your application, make a .NET client library call as follows:
AuthSubUtil.getRequestUrl("http://www.example.com/RetrieveToken",
                          "http://www.google.com/calendar/feeds/",
                                                        false,
                                                        true);

The getRequestUrl method takes several parameters :

  • "next" URL ,which is the URL that Google will redirect to after the user logs into their account and grants access;
  • scope ,as determined in the previous section;
  • two Booleans, one to indicate whether the token will be used in registered mode or not, and one to indicate whether the token will later be exchanged for a session token or not.

The above example shows a call in unregistered mode (the first Boolean is false), for a token that will be exchanged for a session token later (the second Boolean is true); adjust the Booleans appropriately for your application.

After the user follows the link to the AuthSub page at Google and logs in, the AuthSub system then redirects the user back to your application, using the "next" URL you provided.

When Google redirects back to your application, the token is appended to the "next" URL as a query parameter. So in the case of the above "next" URL, after the user logs in, Google redirects to a URL like http://www.example.com/RetrieveToken?token=DQAADKEDE. Therefore, the token is accessible as a variable in the ASP page's Request.QueryString object.

The token you initially retrieve is always a one-time use token. You can exchange this token for a session token using the AuthSubSessionToken URL, as described in the AuthSub documentation. Your application can make this exchange using the .NET client library as follows:

Session["sessionToken"] = 
                AuthSubUtil.exchangeForSessionToken(Request.QueryString["token"], null); 

Now you are ready to use the session token to authenticate requests to the Calendar server by placing the token in the Authorization header.

GAuthSubRequestFactory authFactory = 
                new GAuthSubRequestFactory("cl", "CalendarSampleApp");
authFactory.Token = Session["sessionToken"].ToString();
Service service = new Service("cl", authFactory.ApplicationName);
service.RequestFactory = authFactory; 

To use ClientLogin, invoke the setUserCredentials method of CalendarService, specifying the ID and password of the user on whose behalf your client is sending the query. For example:

CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("jo@gmail.com", "mypassword");

You can get a list of a user's calendars by sending an authenticated GET request to the allcalendars feed URL:

http://www.google.com/calendar/feeds/default/allcalendars/full 

You also can query to retrieve a list of calendars that the authentiated user has access to:

http://www.google.com/calendar/feeds/default/owncalendars/full 

After instantiate a new CalendarService object and authenticate the user, then use the Query method to retrieve a CalendarFeed object that contains entries for all of the user's calendars.

// Create a CalenderService and authenticate
CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
/*The parameter applicationName in the constructor of CalendarService is random,
you can enter any name you want.*/
myService.setUserCredentials("jo@gmail.com", "mypassword");

CalendarQuery query = new CalendarQuery();
query.Uri = new Uri("http://www.google.com/calendar/feeds/default/allcalendars/full");
CalendarFeed resultFeed = service.Query(query);
Console.WriteLine("Your calendars:\n");
foreach (CalendarEntry entry in resultFeed.Entries)
{
    Console.WriteLine(entry.Title.Text + "\n");
}

The CalendarService the key to get the services of Google Calendar,when you finish this,you can get what you want.In the next topic,I will follow that way to get the StartTime of a Calendar Event,and call user using the IVR.

Call user when the event rises up

My idea is first I do a callback to the server side from the client using JavaScript. In the server side, using CalendarService query to retrive the StartTime of the EventEntry from the Calendar Service. Then return the time string to the client, in the client, using the JavaScript method setTimeout to circularly check the returned time with the client time. When they are equal,go back to the server side,construct a IVR object to call user.

This is the html code:

 <head id="Head1" runat="server">
    <title>GetGoogleEvent</title>
    <script language="javascript" type="text/javascript">
    
    var stopTimmer;//Timeout ID
    var strReturn;//The value returned from the server
    
    function ReceiveDateFromServer(valueReturnFromServer)
    {
        document.getElementById("ServerTime").innerHTML = "DateTime of Server:"
        + valueReturnFromServer;
        
        strReturn = valueReturnFromServer;
        
        //Start the reminder to remind the user about the Calendar Event.
        TriggerEvent();
    }
    
    function GetServerTime(format)
    {
        CallBackToServer(format, "");//Send callback to server.
    }
    
    function TriggerEvent()
    {
        //Get the current date with US culture.
        var localDate = Date.parse((new Date()).toString("en-US"));
        document.getElementById('NowTime').innerHTML = "DateTime of Now:" 
                 + (new Date()).toString("en-US");
        
        var serverDate = Date.parse(strReturn);
        var diff = localDate - serverDate;
        //Compare the date returned from server with the client date.
        if (diff >= 0)
        {
            /*If the client is on time,
            then clear the Timeout and start the server event to call user.*/
            
            clearTimeout(stopTimmer);
            
            var btnCall = document.getElementById('btnCall');
            if (btnCall != null)
            {
                btnCall.click();//Call the server event to make a call.
            }
            
            return false;
        }
        
        /*Recurrence,check every 1 second,
         until the current time equals the time returned from Calendar.*/
        stopTimmer = setTimeout("TriggerEvent()", 1000);
    }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnStartReminder" runat="server" Text="Start Reminder" 
        OnClientClick="javascript:GetServerTime('');return false;" />
        <br />
        
        <br />
        <span id="ServerTime">
            DateTime of Now<%= DateTime.Now.ToString("yyyy-MM-dd HHHH:mm:ss") %>
        </span>
        <br />
        
        <span id="NowTime"></span>
        
        <div style="display:none">
            <asp:Button ID="btnCall" runat="server" onclick="CallUser" Text="Button" />
        </div>
    </div>
    </form>
</body>
        

This is the codebehind:

You'll need the following using statements:

using System.Globalization;
using Google.GData.AccessControl;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions; 

In order to do callback,your class must inherit from the ICallbackEventHandler interface.

public partial class CalendarEvent : System.Web.UI.Page, ICallbackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strRefrence = Page.ClientScript.GetCallbackEventReference(
                    this, 
                    "callArgs", 
                    "ReceiveDateFromServer", 
                    "callContext");

                string strCallBack = "function CallBackToServer(callArgs, callContext) {"
                    + strRefrence + "};";

                Page.ClientScript.RegisterClientScriptBlock(
                    this.GetType(), 
                    "CallBackToServer", 
                    strCallBack, 
                    true);
        }

        private string eventMessage = String.Empty;

        public string GetCallbackResult()
        {
            DateTimeFormatInfo myDTFormatInfo = 
                new CultureInfo("en-US", false).DateTimeFormat;
            DateTime eventStartTime = DateTime.Now;
            
            //Create a CalenderService and authenticate
            CalendarService calService = 
                new CalendarService("GoogleCalendarAndIVRSampleApp");
            
            calService.setUserCredentials("userid@gmail.com", "password");

            string feedUri = 
                "http://www.google.com/calendar/feeds/userid@gmail.com/private/full";
            //Get the Calendar Event you wanted.
            Google.GData.Calendar.EventQuery eventQuery = 
                new Google.GData.Calendar.EventQuery(feedUri);
            //This is the title of your wanted event.
            eventQuery.Query = "NewEvent of mine";
            EventFeed resultEventFeed = calService.Query(eventQuery);

            Google.GData.Calendar.EventEntry myEventEntry = 
                (Google.GData.Calendar.EventEntry)resultEventFeed.Entries[0];
            //If you want to remind the user when the event starts,you can do like this:
            eventStartTime = myEventEntry.Times[0].StartTime;

            //And get the summary of the event.
            eventMessage = myEventEntry.Summary.Text;
            //Of cource,you can get the where,etc.

            return eventStartTime.ToString("F", myDTFormatInfo);
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            //You can write some useful code here.
        }

        protected void CallUser(object sender, EventArgs e)
        {
            //Call user
            VoicentIVR callUserByIVR = new VoicentIVR("localhost", 1982);
            callUserByIVR.CallText("User's phone", eventMessage, true);
        }
    } 

The class VoicentIVR is contained in the solution in the ZIP file for downloaded.

Btw,my environment is VS2008,but I don't use the features of .NET Framework 3.5, so I guess these codes can also work fine in VS2005.

Points of Interest

IVR,Google Calendar,Skype

History

IVR introduce

IVR Starter


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jedliu2046

China China
No Biography provided

Comments and Discussions

 
QuestionAll-Day's Calendar Pinmemberzhongzhang8-May-13 0:12 
AnswerRe: All-Day's Calendar Pinmemberzhongzhang8-May-13 0:45 
GeneralHow to make IVR work PinmemberMithunChopda28-Jun-12 15:18 
RantFix formating Pinmemberpsouza4micronet8-Jul-08 5:38 
GeneralRe: Fix formating Pinmemberjedliu20468-Jul-08 17:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 21 Aug 2008
Article Copyright 2008 by jedliu2046
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid