Guest User

Untitled

a guest
Feb 27th, 2018
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. using Microsoft.Rtc.Collaboration;
  2. using Microsoft.Rtc.Signaling;
  3. using System;
  4. using System.Threading;
  5.  
  6. namespace ucma1
  7. {
  8. class Program
  9. {
  10. private const string sipaddress = "sip:sender@domain.com";
  11. private const string username = "sender@domain.com";
  12. private const string password = "senders_password";
  13. private const string domain = "domain";
  14. private const string destinationSip = "sip:receiver@sender.com"; // port: 5061
  15. private const string IMMessage = "Hello! from the bot, just ignore this";
  16.  
  17. private static String _conversationPriority = ConversationPriority.Urgent;
  18. private static String _conversationSubject = "The AskHR Bot!";
  19.  
  20. private static InstantMessagingFlow _instantMessagingFlow;
  21.  
  22. //private InstantMessagingFlow _instantMessagingFlow;
  23. static CollaborationPlatform _collabPlatform { get; set; }
  24. static UserEndpoint _endpoint { get; set; }
  25. static bool _OKToQuit = false;
  26.  
  27. static void Main(string[] args)
  28. {
  29. string userAgent = "ClientPlatformExample";
  30.  
  31. var platformSettings = new ClientPlatformSettings(userAgent, Microsoft.Rtc.Signaling.SipTransportType.Tls);
  32. _collabPlatform = new CollaborationPlatform(platformSettings);
  33.  
  34. //Start up the platform, calling back asynchronously once it's done.
  35. _collabPlatform.BeginStartup(EndCollabPlatformStartup, null);
  36.  
  37. //In this example, wait for everything to finish before exiting
  38. while (!_OKToQuit)
  39. {
  40. System.Threading.Thread.Sleep(2000);
  41. }
  42. }
  43.  
  44. private static void EndCollabPlatformStartup(IAsyncResult ar)
  45. {
  46. _collabPlatform.EndStartup(ar);
  47.  
  48. //A collaboration plaform can have one or more Endpoints. An Endpoint is tied to a SIP Address.
  49. UserEndpointSettings settings = new UserEndpointSettings(sipaddress);
  50. settings.Credential = new System.Net.NetworkCredential(username, password, domain);
  51. settings.AutomaticPresencePublicationEnabled = true;
  52.  
  53. try {
  54. _endpoint = new UserEndpoint(_collabPlatform, settings);
  55. _endpoint.RegisterForIncomingCall<InstantMessagingCall>(OnIncomingCall);
  56.  
  57. _endpoint.BeginEstablish(UserEndpointEstablishCompleted, _endpoint);
  58. }
  59. catch(InvalidOperationException) {
  60. Console.WriteLine("Could not establish endpoint. Platform was not in a valid state.");
  61. }
  62. }
  63.  
  64. private static void OnIncomingCall(object sender, CallReceivedEventArgs<InstantMessagingCall> e) {
  65. try
  66. {
  67. Console.WriteLine(e.RemoteParticipant.Uri);
  68. Console.WriteLine(e.ToastMessage.Message);
  69. }
  70. catch {
  71. Console.WriteLine("e.RemoteParticipant.Uri");
  72. }
  73. }
  74.  
  75. private static void UserEndpointEstablishCompleted(IAsyncResult ar)
  76. {
  77. try {
  78. //LocalEndpoint currentEndpoint = ar.AsyncState as LocalEndpoint;
  79. //currentEndpoint.EndEstablish(ar);
  80. _endpoint.EndEstablish(ar);
  81. }
  82. catch (RealTimeException exception) {
  83. Console.WriteLine("Failed to establish endpoint: {0}", exception.Message);
  84. }
  85.  
  86.  
  87. // Setup the conversation and place the call.
  88. ConversationSettings convSettings = new ConversationSettings();
  89. convSettings.Priority = _conversationPriority;
  90. convSettings.Subject = _conversationSubject;
  91.  
  92. //Once the endpoint is in place, create a Conversation and an IM Call.
  93. var Conversation = new Conversation(_endpoint, convSettings);
  94. var Call = new InstantMessagingCall(Conversation);
  95.  
  96. Call.StateChanged += InstantMessagingCall_StateChanged;
  97.  
  98. //When the call is established, Flow will be created. Flow is how you sent IMs around. Therefore, just before
  99. //establishing, we attach an event handler to catch the flow being setup (it's state will change to Active)
  100. Call.InstantMessagingFlowConfigurationRequested += Call_InstantMessagingFlowConfigurationRequested;
  101.  
  102. Call.BeginEstablish(destinationSip, new CallEstablishOptions(), EndBeginEstablish, Call);
  103. }
  104.  
  105. // Just to record the state transitions in the console.
  106. static void InstantMessagingCall_StateChanged(object sender, CallStateChangedEventArgs e)
  107. {
  108. Console.WriteLine("Call has changed state. The previous call state was: " + e.PreviousState +
  109. "and the current state is: " + e.State);
  110. }
  111.  
  112. private static void EndBeginEstablish(IAsyncResult ar)
  113. {
  114. Call call = (Call)ar.AsyncState;
  115. call.EndEstablish(ar);
  116. }
  117.  
  118. static void Call_InstantMessagingFlowConfigurationRequested(object sender, InstantMessagingFlowConfigurationRequestedEventArgs e)
  119. {
  120. //InstantMessagingFlow _instantMessagingFlow = e.Flow;
  121. _instantMessagingFlow = e.Flow;
  122. //Once we're notified about this, we get a handle to the newly created Flow.
  123. //Let's use this to register for state changes.
  124. _instantMessagingFlow.StateChanged += Flow_StateChanged;
  125.  
  126. // Message Received is the event used to indicate that a message has
  127. // been received from the far end.
  128. _instantMessagingFlow.MessageReceived += Flow_MessageReceived;
  129.  
  130. // typing
  131. _instantMessagingFlow.RemoteComposingStateChanged +=
  132. InstantMessagingFlow_RemoteComposingStateChanged;
  133. }
  134.  
  135. static void InstantMessagingFlow_RemoteComposingStateChanged(object sender,
  136. ComposingStateChangedEventArgs e)
  137. {
  138. // Prints the typing notifications of the far end user.
  139. Console.WriteLine("Participant "
  140. + e.Participant.Uri.ToString()
  141. + " is "
  142. + e.ComposingState.ToString()
  143. );
  144. }
  145.  
  146. static void Flow_MessageReceived(object sender, InstantMessageReceivedEventArgs e) {
  147. Console.WriteLine(e.Sender.Uri + " said: " + e.TextBody);
  148. _instantMessagingFlow.LocalComposingState = ComposingState.Composing;
  149. Thread.Sleep(2000);
  150. if (e.TextBody.ToLower().Contains("bye"))
  151. {
  152. _instantMessagingFlow.BeginSendInstantMessage("Echo: " + e.TextBody, EndBeginSendInstanceMessage, _instantMessagingFlow);
  153. }
  154. else {
  155. _instantMessagingFlow.BeginSendInstantMessage("Echo: " + e.TextBody, GoOn, _instantMessagingFlow);
  156. }
  157.  
  158. }
  159.  
  160. static void GoOn(IAsyncResult ar) {
  161. _instantMessagingFlow.LocalComposingState = ComposingState.Idle;
  162. }
  163.  
  164. static void Flow_StateChanged(object sender, MediaFlowStateChangedEventArgs e)
  165. {
  166. if (e.State == MediaFlowState.Active)
  167. {
  168. //The flow is now active! We can use it to send messages.
  169. //InstantMessagingFlow flow = (InstantMessagingFlow)sender;
  170. //flow.BeginSendInstantMessage(IMMessage, EndBeginSendInstanceMessage, flow);
  171. _instantMessagingFlow.BeginSendInstantMessage(IMMessage, GoOn, _instantMessagingFlow);
  172. }
  173. }
  174.  
  175. private static void EndBeginSendInstanceMessage(IAsyncResult ar)
  176. {
  177. _instantMessagingFlow.LocalComposingState = ComposingState.Idle;
  178. InstantMessagingFlow flow = (InstantMessagingFlow)ar.AsyncState;
  179. flow.EndSendInstantMessage(ar);
  180.  
  181. //Having sent the message, terminate the conversation
  182. flow.Call.Conversation.BeginTerminate(EndBeginTerminate, flow.Call.Conversation);
  183. }
  184.  
  185. private static void EndBeginTerminate(IAsyncResult ar)
  186. {
  187. Conversation conversation = (Conversation)ar.AsyncState;
  188. conversation.EndTerminate(ar);
  189.  
  190. //_OKToQuit = true;
  191. }
  192.  
  193. }
  194. }
Add Comment
Please, Sign In to add comment