C#

This application will demonstrate how to implement endpoints to start receiving callbacks from Live Link 365.

This is not a production-ready application. Please take your time to read about ASP.NET MVC to make it meet production standards and your business requirements.

To follow this tutorial, you should have a basic understanding of:
  • C#
  • ASP.NET MVC

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace callbacks
{
    // _Step_1
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Environment.SetEnvironmentVariable("DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER", "true");
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// _Step_2
{
    "Logging": {
        "IncludeScopes": true,
        "Debug": {
            "LogLevel": {
                "Default": "Debug",
                "System": "Information",
                "Microsoft": "Information"
            }
        },
        "Console": {
            "LogLevel": {
                "Default": "Information"
            }
        }
    },
    "LIVELINK_APP_KEY": "",
    "LIVELINK_APP_SECRET": "",
    "LIVELINK_BASE_URL": "https://livelink.sapdigitalinterconnect.com/api",
    "LIVELIN_DEFAULT_ORIGIN": ""
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
namespace callbacks.Models
{
    // _Step_3
    public class StatusBody
    {
        public String messageId { set; get; }
        public String message { set; get; }
        public String msisdn { set; get; }

        public StatusBody()
        {
        }

        public StatusBody(string messageId, string message, string msisdn)
        {
            this.messageId = messageId;
            this.message = message;
            this.msisdn = msisdn;
        }
    }
}
 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
using System;
namespace callbacks.Models
{
// _Step_4
    public class IncomingBody
    {

        public String messageId { set; get; }
        public String message { set; get; }
        public String msisdn { set; get; }
        public String originatingAddress { set; get; }
        public Parameters parameters { set; get; }
        public ReceivedTime receivedTime { set; get; }

        public class Code
        {
            public String mcc { set; get; }
            public String mnc { set; get; }

            public Code() { }

            public Code(String mcc, String mnc)
            {
                this.mnc = mnc;
                this.mcc = mcc;
            }
        }

        public class Operator
        {
            public Code code { set; get; }
            public String standard { set; get; }

            public Operator() { }

            public Operator(Code code, String standard)
            {
                this.code = code;
                this.standard = standard;
            }
        }

        public class ReceivedTime
        {
            public String Date { set; get; }
            public String Time { set; get; }

            public ReceivedTime() { }

            public ReceivedTime(String Date, String Time)
            {
                this.Date = Date;
                this.Time = Time;
            }
        }

        public class Parameters
        {
            public String accountId { set; get; }
            public String dcs { set; get; }
            public String keyword { set; get; }
            public String messageClass { set; get; }
            public String messageId { set; get; }
            public Operator @operator { set; get; }
            public String receivedServiceNumber { set; get; }
            public ReceivedTime receivedTime { set; get; }
            public String udh { set; get; }

            public Parameters() { }

            public Parameters(String accountId, String dcs, String keyword, String messageClass, String messageId, Operator @operator, String receivedServiceNumber, ReceivedTime receivedTime, String udh)
            {
                this.accountId = accountId;
                this.dcs = dcs;
                this.keyword = keyword;
                this.messageClass = messageClass;
                this.messageId = messageId;
                this.@operator = @operator;
                this.receivedServiceNumber = receivedServiceNumber;
                this.receivedTime = receivedTime;
                this.udh = udh;
            }
        }

        public IncomingBody()
        {
        }
    }
}
 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using callbacks.Models;
using callbacks.Services;

namespace callbacks.Controllers
{

    // _Step_5
    public class CallbackController : Controller
    {
        private ILogger logger;

        public CallbackController(IConfiguration configuration, ILogger<CallbackController> logger)
        {
            this.logger = logger;
        }

        [Route("messages/status")]
        public void ReceiveMessageStatus([FromBody]StatusBody statusBody)
        {
            if (this.Request.Method.Equals("POST")) {
                logger.LogInformation("Message #{MessageId} changed status to: {Status}", statusBody.messageId, statusBody.message);
            }
        }

        [Route("messages/incoming")]
        public void ReceiveIncomingMessage([FromBody]IncomingBody incomingMessageBody)
        {
            if (this.Request.Method.Equals("POST")) {
                logger.LogInformation("\nMessage Response for message order {0}\nFrom Phone Number: {1}\nResponse Text: {2}\nOperator Standard: {3}\nReceived on {4} at {5}",
                                                incomingMessageBody.messageId,
                                                incomingMessageBody.msisdn,
                                                incomingMessageBody.message,
                                                incomingMessageBody.parameters.@operator.standard,
                                                incomingMessageBody.receivedTime.Date,
                                                incomingMessageBody.receivedTime.Time);
            }
        }
    }
}
 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
using System;
using callbacks.Models;

namespace callbacks.Services
{
// _Step_6
    public class AppointmentService
    {
        private MessageHandler messageHandler;

        public AppointmentService()
        {
        }

        public AppointmentService(MessageHandler messageHandler)
        {
            this.messageHandler = messageHandler;
        }

        public void sendAppointmentMessage()
        {
            Doctor doctor = new Doctor("1", "Christina", "Yang", "Cardiology");
            Person patient = new Person("2", "Steven", "Gorwell", "+50431401307");
            DateTime bookingDate = new DateTime(2018, 06, 18, 16, 0, 0);

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

            String message = String.Format("Mr/Ms %s, you have an appointment with Dr. %s (%s), schedule on %s at %s.",
                                        appointment.Patient.getFullName(),
                                        appointment.Doctor.getFullName(),
                                        appointment.Doctor.Specialization,
                                        appointment.Date.ToString("D"),
                                        appointment.Date.ToString("t"));
            messageHandler.sendMessage(message, appointment.Patient.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
using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using callbacks.Models;
using callbacks.Services;

namespace callbacks
{
    public class Program
    {
        // _Step_7
        public static void Main(string[] args)
        {
            var host = BuildWebHostBuilder(args).Build();
            using (var scope = host.Services.CreateScope()) {
                var services = scope.ServiceProvider;
                IConfiguration configuration = services.GetRequiredService<IConfiguration>();
                MessageHandler messageHandler = new MessageHandler(configuration["LIVELINK_BASE_URL"], configuration["LIVELINK_APP_KEY"], configuration["LIVELINK_APP_SECRET"], configuration["LIVELIN_DEFAULT_ORIGIN"]);
                AppointmentService appointmentService = new AppointmentService(messageHandler);
                appointmentService.sendAppointmentMessage();
            }
        }

        public static IWebHostBuilder BuildWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}