C#

This tutorial provides you with a step-by-step walkthrough of how to build a ‘Doctor’s Appointment App’ to send appointment reminders via SMS using Live Link 365.

This is not a production-ready application. Please take your time to enhance it for production so that it meets your specific business requirements.

The main panel on the right highlights the right piece of code for each step.

Steps

Code

keyboard_arrow_down
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
namespace appointmentReminder.Class
{

    // _Step_1
    public class Person
    {
        private String id;
        private String firstName;
        private String lastName;
        private String phoneNumber; //Phone numbers have to be in E.164 format.


        public Person()
        {
        }

        public Person(String id, String firstName, String lastName, String phoneNumber)
        {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
            this.phoneNumber = phoneNumber;
        }

        public String getFullName()
        {
            return this.firstName + " " + this.lastName;
        }

        public String Id { get => id; set => id = value; }
        public String FirstName { get => firstName; set => firstName = value; }
        public String LastName { get => lastName; set => lastName = value; }
        public String PhoneNumber { get => phoneNumber; set => phoneNumber = value; }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
namespace appointmentReminder.Class
{

    // _Step_2
    public class Doctor : Person
    {
        private Specialization specialization;

        public Specialization Specialization { get => specialization; set => specialization = value; }

        public Doctor()
        {
        }

        public Doctor(String id, String firstName, String lastName, Specialization specialization)
        {
            this.Id = id;
            this.FirstName = firstName;
            this.LastName = lastName;
            this.specialization = specialization;
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
namespace appointmentReminder.Class
{
    // _Step_3
    public class Specialization
    {
        private Int32 id;
        private String name;

        public Specialization()
        {
        }

        public Specialization(Int32 id, String name)
        {
            this.id = id;
            this.name = name;
        }

        public Int32 Id { get => id; set => id = value; }
        public String Name { get => name; set => name = value; }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;

namespace appointmentReminder.Class
{
    // _Step_4
    public class Appointment
    {
        private Doctor doctor;
        private Person patient;
        private DateTime date;
        private Specialization specialization;

        public Appointment()
        {
        }

        public Appointment(Doctor doctor, Person patient, DateTime date)
        {
            this.doctor = doctor;
            this.patient = patient;
            this.date = date;
            this.specialization = doctor.Specialization;
        }

        public Doctor Doctor { get => doctor; set => doctor = value; }
        public Person Patient { get => patient; set => patient = value; }
        public DateTime Date { get => date; set => date = value; }
        public Specialization Specialization { get => specialization; set => specialization = value; }
    }
}
1
2
3
4
5
6
/* _Step_5 */
{
    "appKey": "",
    "appSecret": "",
    "defaultOrigin": ""
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using RestSharp;

namespace appointmentReminder.Class
{
    public class MessageHandler
    {
        private String baseUrl = "https://livelink.sapdigitalinterconnect.com/api";
        private String appKey;
        private String appSecret;
        private String defaultOrigin;
        private WebProxy globalProxy;

        public MessageHandler()
        {
        }

        public MessageHandler(String appKey, String appSecret, String defaultOrigin = "", WebProxy proxy = null)
        {
            this.appKey = appKey;
            this.appSecret = appSecret;
            this.defaultOrigin = defaultOrigin;
            this.globalProxy = proxy;
        }

        // _Step_6
        public String GetAccessToken()
        {
            String oAuthUrl = this.baseUrl + "/oauth/token";
            String bearer = Convert.ToBase64String(Encoding.UTF8.GetBytes(this.appKey + ":" + this.appSecret));
            RestClient client = new RestClient(oAuthUrl);
            if (this.globalProxy != null) {
                client.Proxy = this.globalProxy;
                client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }
            RestRequest request = new RestRequest(Method.POST);
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            request.AddHeader("authorization", "Basic " + bearer);
            request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials", ParameterType.RequestBody);

            IRestResponse<oAuthToken> response = client.Execute<oAuthToken>(request);

            if (response.StatusCode != HttpStatusCode.OK) {
                Console.WriteLine("..... Error: " + response.StatusDescription);
                return "";
            }

            String accessToken = response.Data.access_token;
            return accessToken;
        }

        public void sendMessage(String message, String destination, String origin = "")
        {
            String messageUrl = this.baseUrl + "/v2/sms";
            Object messagePayload = new {
                message = message,
                origin =  origin == "" ? this.defaultOrigin : origin,
                destination = destination //Phone numbers have to be in E.164 format.
            };
            String messageData = JsonConvert.SerializeObject(messagePayload);
            String bearer = GetAccessToken();
            if (String.IsNullOrEmpty(bearer)) {
                return;
            }
            RestClient client = new RestClient(messageUrl);
            if (this.globalProxy != null) {
                client.Proxy = this.globalProxy;
                client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }

            RestRequest request = new RestRequest(Method.POST);
            request.AddHeader("authorization", "Bearer " + bearer);
            request.AddHeader("content-type", "application/json");
            request.AddParameter("application/json", messageData, ParameterType.RequestBody);

            IRestResponse response = client.Execute(request);

            if (response.StatusCode != HttpStatusCode.OK) {
                Console.WriteLine("..... failed to send message.");
                Console.WriteLine("..... Error: " + response.ErrorMessage);
            }
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using appointmentReminder.Class;

namespace appointmentReminder
{
    class MainClass
    {

        // _Step_7
        public static void Main(string[] args)
        {
            JObject properties = JObject.Parse(File.ReadAllText(@"../../Resources/properties.json"));

            MessageHandler messageHandler = new MessageHandler(properties.GetValue("appKey").ToString(), properties.GetValue("appSecret").ToString(), properties.GetValue("defaultOrigin").ToString());

            Specialization cardio = new Specialization(1, "Cardiology");
            Doctor doctor = new Doctor("1", "Christina", "Yang", cardio);
            Person patient = new Person("2", "Steven", "Gorwell", "+2349092054");
            DateTime bookingDate = new DateTime(2018, 06, 18, 16, 0, 0);

            Appointment appointment = new Appointment(doctor, patient, bookingDate);

            String message = "Mr/Ms " + appointment.Patient.getFullName() + ", you have an appointment with Dr. " + appointment.Doctor.getFullName()
                                                   + "(" + appointment.Specialization.Name + ") scheduled on " + appointment.Date.ToString("D") + " at "
                                                   + appointment.Date.ToString("t");
            Console.WriteLine("Sending appointment reminder...");
            messageHandler.sendMessage(message, appointment.Patient.PhoneNumber);
        }
    }
}