CallingConversation.cs
1 using Autofac;
2 using System;
3 using System.Diagnostics;
4 using System.Net;
5 using System.Net.Http;
6 using System.Text;
7 using System.Threading.Tasks;
8 
9 namespace Microsoft.Bot.Builder.Calling
10 {
14  public interface ICallingBot
15  {
17  }
18 
22  public static partial class CallingConversation
23  {
24  public static readonly IContainer Container;
25 
26  static CallingConversation()
27  {
28  var builder = new ContainerBuilder();
29  builder.RegisterModule(new CallingModule_MakeBot());
30  Container = builder.Build();
31  }
32 
37  public static void RegisterCallingBot(Func<ICallingBotService, ICallingBot> MakeCallingBot)
38  {
39  CallingModule_MakeBot.Register(Container, MakeCallingBot);
40  }
41 
48  public static async Task<HttpResponseMessage> SendAsync(HttpRequestMessage toBot, CallRequestType callRequestType)
49  {
50  using (var scope = CallingModule.BeginLifetimeScope(Container, toBot))
51  {
52  var bot = scope.Resolve<ICallingBot>();
53  var context = scope.Resolve<CallingContext>();
54  var parsedRequest = await context.ProcessRequest(callRequestType);
55  Trace.TraceInformation($"Processing X-Microsoft-Skype-Chain-ID: {parsedRequest.SkypeChaindId}");
56  if (parsedRequest.Faulted())
57  {
58  return context.Request.CreateResponse(parsedRequest.ParseStatusCode);
59  }
60  else
61  {
62  try
63  {
64  string res = string.Empty;
65  switch (callRequestType)
66  {
67  case CallRequestType.IncomingCall:
68  res = await bot.CallingBotService.ProcessIncomingCallAsync(parsedRequest.Content);
69  break;
70  case CallRequestType.CallingEvent:
71  res = await bot.CallingBotService.ProcessCallbackAsync(parsedRequest.Content, parsedRequest.AdditionalData);
72  break;
73  default:
74  throw new Exception($"Unsupported call request type: {callRequestType}");
75  }
76 
77  return new HttpResponseMessage(HttpStatusCode.OK)
78  {
79  Content = new StringContent(res, Encoding.UTF8, "application/json")
80  };
81  }
82  catch (Exception e)
83  {
84  return context.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
85  }
86  }
87  }
88 
89 
90  }
91  }
92 }
static void RegisterCallingBot(Func< ICallingBotService, ICallingBot > MakeCallingBot)
Register the instance of calling bot responsible for handling the calling requests.
The context for this request. It is reponsible in parsing the calling request before calling into ICa...
static async Task< HttpResponseMessage > SendAsync(HttpRequestMessage toBot, CallRequestType callRequestType)
Process a calling request within the calling conversaion.
static ILifetimeScope BeginLifetimeScope(ILifetimeScope scope, HttpRequestMessage request)
The top level composition root for calling SDK.
static void Register(ILifetimeScope scope, Func< ICallingBotService, ICallingBot > MakeCallingBot)
virtual async Task< ParsedCallingRequest > ProcessRequest(CallRequestType callType)
Process the calling request and returns ParsedCallingRequest.
CallRequestType
The type of calling request
Autofac module for Calling components.