C#

This guide provides a step-by-step walkthrough of how to use Live Link 365 to build a ‘Bank Customer Service App’ to send updates using SMS.

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
using System;
namespace bankProductAdvertising.Class
{
    // _Step_1
    public class Person
    {
        private String id;
        private String userName;
        private String firstName;
        private String lastName;
        private String phoneNumber;


        public Person()
        {
        }

        public Person(String id, String userName, String firstName, String lastName, String phoneNumber)
        {
            this.id = id;
            this.userName = userName;
            this.firstName = firstName;
            this.lastName = lastName;
            this.phoneNumber = phoneNumber; //Phone numbers have to be in E.164 format.
        }

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

        public String Id { get => id; set => id = value; }
        public String UserName { get => userName; set => userName = 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
25
26
27
28
29
30
31
32
using System;
using System.Collections.Generic;

namespace bankProductAdvertising.Class
{
    // _Step_2
    public class Customer : Person
    {

        private List<Product> products;

        public Customer()
        {
        }

        public Customer(String id, String firstName, String lastName, String phoneNumber)
        {
            this.Id = id;
            this.FirstName = firstName;
            this.LastName = lastName;
            this.PhoneNumber = phoneNumber; //Phone numbers have to be in E.164 format.
            this.products = new List<Product>();
        }

        public void addProduct(Product product)
        {
            this.products.Add(product);
        }

        public List<Product> Products { get => products; set => products = 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 bankProductAdvertising.Class
{
    // _Step_3
    public class BankSalesPerson : Person
    {
        private String department;

        public BankSalesPerson()
        {
        }

        public BankSalesPerson(String id, String firstName, String lastName, String department, String phoneNumber)
        {
            this.Id = id;
            this.FirstName = firstName;
            this.LastName = lastName;
            this.department = department;
            this.PhoneNumber = phoneNumber; //Phone numbers have to be in E.164 format.
        }

        public string Department { get => department; set => department = 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 bankProductAdvertising.Class
{
    // _Step_4
    public class Product
    {
        private String id;
        private String name;


        public Product()
        {
        }

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

        public string 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
using System;
namespace bankProductAdvertising.Class
{
    // _Step_5
    public class Complaint
    {
        private String customerId;
        private String type;
        private String content;

        public Complaint()
        {
        }

        public Complaint(String customerId, String type, String content)
        {
            this.customerId = customerId;
            this.type = type;
            this.content = content;
        }

        public string CustomerId { get => customerId; set => customerId = value; }
        public string Type { get => type; set => type = value; }
        public string Content { get => content; set => content = 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
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
using System;
using System.Collections.Generic;
using System.IO;

namespace bankProductAdvertising.Class
{
    // _Step_6
    public class ComplaintSystem
    {
        private Dictionary<String, Customer> customerDetails;
        private Dictionary<String, BankSalesPerson> bankSalesPersonDetails;
        private List<Complaint> complaints;
        private MessageHandler messageHandler;

        public ComplaintSystem(UploadDetails details, MessageHandler messageHandler)
        {
            this.bankSalesPersonDetails = details.BankSalesPersonDetail;
            this.customerDetails = details.CustomerDetail;
            this.messageHandler = messageHandler;
            this.complaints = new List<Complaint>();
            this.loadComplaints();
        }

        public ComplaintSystem()
        {
        }

        private void loadComplaints()
        {
            using (StreamReader reader = new StreamReader(@"../../Resources/CustomerComplaints.csv")) {
                while (!reader.EndOfStream) {
                    String line = reader.ReadLine();
                    String[] values = line.Split('|');
                    Complaint complaint = new Complaint(values[0], values[1], values[2]);
                    this.complaints.Add(complaint);
                }
            }
        }

        public void updateComplaints()
        {
            foreach (Complaint complaint in this.complaints) {
                Customer customer;
                BankSalesPerson bankSalesPerson = this.getBankPerson(complaint.Type);
                String message;
                this.customerDetails.TryGetValue(complaint.CustomerId, out customer);
                if (complaint.Type.Equals("COMPLAINT")) {
                    message = "Hi Mr/Ms" + customer.getFullName() + ", on your complaint: " + complaint.Content + ", Mr/Ms " + bankSalesPerson.getFullName() + " will contact you with phone number: " + bankSalesPerson.PhoneNumber;
                } else if (complaint.Type.Equals("QUERY")) {
                    message = "Hi Mr/Ms" + customer.getFullName() + ", on your query: " + complaint.Content + ". Please connect to our sales person Mr/Ms " + bankSalesPerson.getFullName() + " on " + bankSalesPerson.PhoneNumber + " or our sales person will conect with you in the next 3 working days.";
                } else if (complaint.Type.Equals("CRITICAL")) {
                    message = "Hi Mr/Ms" + customer.getFullName() + ", on your issue: " + complaint.Content + ", an appointment with our manager is being fixed on " + this.getDateAndTime() + ".";
                } else {
                    message = "";
                }
                if (!message.Equals("")) {
                    messageHandler.sendMessage(message, customer.PhoneNumber);
                }
            }
        }

        private BankSalesPerson getBankPerson(String complaintType)
        {
            BankSalesPerson salesPerson;
            if (complaintType.Equals("COMPLAINT")) {
                bankSalesPersonDetails.TryGetValue("3", out salesPerson);
            } else if (complaintType.Equals("QUERY")) {
                bankSalesPersonDetails.TryGetValue("2", out salesPerson);
            } else if (complaintType.Equals("CRITICAL")) {
                bankSalesPersonDetails.TryGetValue("1", out salesPerson);
            } else {
                salesPerson = null;
            }
            return salesPerson;
        }

        private String getDateAndTime()
        {
            DateTime sysDate = DateTime.Now;
            Int32 days = (sysDate.DayOfWeek == DayOfWeek.Sunday || sysDate.DayOfWeek == DayOfWeek.Saturday) ? 3 : 1;
            DateTime appointment = DateTime.Now.AddDays(days);
            return appointment.ToString("D");
        }
    }
}
 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
using System;
using System.Collections.Generic;
using System.IO;

namespace bankProductAdvertising.Class
{
    // _Step_7
    public class UploadDetails
    {
        private Dictionary<String, Customer> customerDetail;
        private Dictionary<String, Product> productDetail;
        private Dictionary<String, BankSalesPerson> bankSalesPersonDetail;

        public Dictionary<String, Customer> CustomerDetail { get => customerDetail; set => customerDetail = value; }
        public Dictionary<String, Product> ProductDetail { get => productDetail; set => productDetail = value; }
        public Dictionary<String, BankSalesPerson> BankSalesPersonDetail { get => bankSalesPersonDetail; set => bankSalesPersonDetail = value; }

        public UploadDetails()
        {
            Console.WriteLine("Loading...");
            this.customerDetail = new Dictionary<String, Customer>();
            this.productDetail = new Dictionary<String, Product>();
            this.bankSalesPersonDetail = new Dictionary<String, BankSalesPerson>();
            loadProductDetails();
            loadCustomerDetails();
            loadBankSalesDetails();
        }

        public void loadCustomerDetails()
        {
            using (StreamReader reader = new StreamReader(@"../../Resources/CustomerList.csv")) {
                while (!reader.EndOfStream) {
                    String line = reader.ReadLine();
                    String[] values = line.Split('|');
                    Customer customer = new Customer(values[0], values[1], values[2], values[3]);
                    String[] products = values[4].Split(',');
                    for (Int32 i = 0; i < products.Length; i++) {
                        customer.addProduct(this.getProductById(products[i]));
                    }
                    this.customerDetail.Add(customer.Id, customer);
                }
            }

        }

        public void loadBankSalesDetails()
        {
            using (StreamReader reader = new StreamReader(@"../../Resources/BankSalesList.csv")) {
                while (!reader.EndOfStream) {
                    String line = reader.ReadLine();
                    String[] values = line.Split('|');
                    BankSalesPerson bankSalesPerson = new BankSalesPerson(values[0], values[1], values[2], values[3], values[4]);
                    this.bankSalesPersonDetail.Add(bankSalesPerson.Id, bankSalesPerson);
                }
            }
        }

        public void loadProductDetails()
        {
            using (StreamReader reader = new StreamReader(@"../../Resources/ProductsList.csv")) {
                while (!reader.EndOfStream) {
                    String line = reader.ReadLine();
                    String[] values = line.Split('|');
                    Product product = new Product(values[0], values[1]);
                    this.productDetail.Add(product.Id, product);
                }
            }
        }

        private Product getProductById(String productId)
        {
            Product productToReturn = null;
            this.productDetail.TryGetValue(productId, out productToReturn);
            return productToReturn;
        }
    }
}
 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
using System;
using System.Collections;
using System.Collections.Generic;

namespace bankProductAdvertising.Class
{
    // _Step_8
    public class Advertisement
    {
        private Dictionary<String, Customer> customerDetailMap;
        private Dictionary<String, Product> customerProductMap;
        private MessageHandler messageHandler;

        public Advertisement(UploadDetails details, MessageHandler messageHandler)
        {
            this.customerDetailMap = details.CustomerDetail;
            this.customerProductMap = details.ProductDetail;
            this.messageHandler = messageHandler;
        }

        public Advertisement()
        {
        }

        public void creditCardAdvertisement()
        {
            foreach (KeyValuePair<String, Customer> customer in this.customerDetailMap) {
                if (customer.Value.Products.Find((obj) => obj.Name == "CREDIT_CARD") != null && customer.Value.Products.Find((obj) => obj.Name == "PREMIUM_CARD") == null) {
                    String message = "Hi Mr/Ms" + customer.Value.getFullName() + ", we are offering a new Premium Credit Card for you. Upgrade your card to Premium Card with lots of benefits.";
                    messageHandler.sendMessage(message, customer.Value.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
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 bankProductAdvertising.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_9
        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
using System.IO;
using bankProductAdvertising.Class;
using Newtonsoft.Json.Linq;

namespace bankProductAdvertising
{
    // _Step_10
    class MainClass
    {
        public static void Main(string[] args)
        {
            JObject properties = JObject.Parse(File.ReadAllText(@"../../configuration.json"));
            MessageHandler messageHandler = new MessageHandler(properties.GetValue("appKey").ToString(), properties.GetValue("appSecret").ToString(), properties.GetValue("defaultOrigin").ToString());
            UploadDetails details = new UploadDetails();
            Advertisement ads = new Advertisement(details, messageHandler);
            ComplaintSystem complaints = new ComplaintSystem(details, messageHandler);

            ads.creditCardAdvertisement();//Send a message to all customers who have a Credit card but no a Premium card
            complaints.updateComplaints();//Send a message to all customers who have reached the complaint system.
        }
    }
}