C#

This is a walkthrough showing you how to connect Live Link 365's SMS-sending API to a flight booking app. This sample creates the model of a flight's booking information and sends an SMS notification to a phone number.

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

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
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
88
89
90
91
using System;
using System.Net;
using System.Text;

// _Step_1
using Newtonsoft.Json;
using RestSharp;

namespace flightBooking.Class
{

    public class MessageHandler
    {

        private String baseUrl = "http://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 = "") //Phone numbers have to be in E.164 format.
        {
            String messageUrl = this.baseUrl + "/v2/sms";
            Object messagePayload = new {
                message = message,
                origin =  origin == "" ? this.defaultOrigin : origin,
                destination = destination
            };
            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
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
using System;
namespace flightBooking.Class
{

    // _Step_2
    public class Flight
    {

        private String id;
        private String from;
        private String to;
        private String departureTime;
        private String arriveTime;
        private Int32 numberOfSeats;
        private Int32 availableSeats;

        // _Step_3
        private MessageHandler messageHandler = new MessageHandler("", "", "");// Insert here your appkey, appSecret and default origin respectively.

        public String Id { get => id; set => id = value; }
        public String From { get => from; set => from = value; }
        public String To { get => to; set => to = value; }
        public String DepartureTime { get => departureTime; set => departureTime = value; }
        public String ArriveTime { get => arriveTime; set => arriveTime = value; }
        public Int32 NumberOfSeats { get => numberOfSeats; set => numberOfSeats = value; }
        public Int32 AvailableSeats { get => availableSeats; set => availableSeats = value; }



        public Flight()
        {
        }

        public Flight(String id, String from, String to, String departureTime, String arriveTime, Int32 numberOfSeats = 10)
        {
            this.id = id;
            this.from = from;
            this.to = to;
            this.departureTime = departureTime;
            this.arriveTime = arriveTime;
            this.numberOfSeats = numberOfSeats;
            this.availableSeats = numberOfSeats;
        }

        public String getAvailableSeatsAndAssign(Int32 numberOfSeatsToBook)
        {
            if (numberOfSeatsToBook > this.availableSeats) {
                return "Seats amount to book is higher than the seats avalaible for flight " + this.id;
            }
            this.availableSeats -= numberOfSeatsToBook;
            return this.id + "-" + this.availableSeats;

        }

        public void bookFlight(Customer customer)
        {
            String message = "Hi " + customer.FirstName + " your flight has been booked with seat number " + getAvailableSeatsAndAssign(1) +
                                             ", from: " + this.from + ", to: " + this.to + ", departuring at " + this.departureTime;
            messageHandler.sendMessage(message, customer.PhoneNumber);
        }

        public void checkIfFlightIsDelayed(String newDepartureTime, Customer customer)
        {
            if (this.DepartureTime != newDepartureTime) {
                String message = "Flight " + this.id + " from: " + this.from + " to: " + this.to + " is delayed, the new departure time will be at " + newDepartureTime;
                messageHandler.sendMessage(message, customer.PhoneNumber);
            }
        }
    }
}
 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
using System;
namespace flightBooking.Class
{

    // _Step_4
    public class Flight
    {

        private String id;
        private String from;
        private String to;
        private String departureTime;
        private String arriveTime;
        private Int32 numberOfSeats;
        private Int32 availableSeats;
        private MessageHandler messageHandler = new MessageHandler("", "", "");// Insert here your appkey, appSecret and default origin respectively

        public String Id { get => id; set => id = value; }
        public String From { get => from; set => from = value; }
        public String To { get => to; set => to = value; }
        public String DepartureTime { get => departureTime; set => departureTime = value; }
        public String ArriveTime { get => arriveTime; set => arriveTime = value; }
        public Int32 NumberOfSeats { get => numberOfSeats; set => numberOfSeats = value; }
        public Int32 AvailableSeats { get => availableSeats; set => availableSeats = value; }



        public Flight()
        {
        }

        public Flight(String id, String from, String to, String departureTime, String arriveTime, Int32 numberOfSeats = 10)
        {
            this.id = id;
            this.from = from;
            this.to = to;
            this.departureTime = departureTime;
            this.arriveTime = arriveTime;
            this.numberOfSeats = numberOfSeats;
            this.availableSeats = numberOfSeats;
        }

        public String getAvailableSeatsAndAssign(Int32 numberOfSeatsToBook)
        {
            if (numberOfSeatsToBook > this.availableSeats) {
                return "Seats amount to book is higher than the seats avalaible for flight " + this.id;
            }
            this.availableSeats -= numberOfSeatsToBook;
            return this.id + "-" + this.availableSeats;

        }

        public void bookFlight(Customer customer)
        {
            String message = "Hi " + customer.FirstName + " your flight has been booked with seat number " + getAvailableSeatsAndAssign(1) +
                                             ", from: " + this.from + ", to: " + this.to + ", departuring at " + this.departureTime;
            messageHandler.sendMessage(message, customer.PhoneNumber);
        }

        public void checkIfFlightIsDelayed(String newDepartureTime, Customer customer)
        {
            if (this.DepartureTime != newDepartureTime) {
                String message = "Flight " + this.id + " from: " + this.from + " to: " + this.to + " is delayed, the new departure time will be at " + newDepartureTime;
                messageHandler.sendMessage(message, customer.PhoneNumber);
            }
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// _Step_5
[
    {
        "id": "UNI101",
        "from": "New York",
        "to": "Chicago",
        "departureTime": "11:30AM",
        "arriveTime": "01:00PM",
        "numberOfSeats": 50,
        "availableSeats": 3
    },
    {
        "id": "AUX201",
        "from": "Boston",
        "to": "Miami",
        "departureTime": "11:10AM",
        "arriveTime": "02:00PM",
        "numberOfSeats": 12,
        "availableSeats": 8
    }
]
 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
using System;
using Newtonsoft.Json;
using System.IO;
using System.Collections.Generic;
using flightBooking.Class;

namespace flightBooking
{
    class MainClass
    {
        // _Step_7
        public static void Main(string[] args)
        {
            List<Flight> flights;
            Customer customer = new Customer("SAMS", "Sam", "Smith", "+132948392423");//Create a customer, asign an id, first name, last name and the phone number to send the message
            using (StreamReader info = File.OpenText(@"../../Resources/flights.json")) { //Retrieve the flight information
                JsonSerializer serializer = new JsonSerializer();
                flights = (List<Flight>)serializer.Deserialize(info, typeof(List<Flight>));
            }

            flights[0].bookFlight(customer);//Book a seat for the customer
            flights[1].checkIfFlightIsDelayed("11:50AM", customer);//Check if customer flight is delayed
        }
    }
}