Skip to content

Instantly share code, notes, and snippets.

@AbhishekAshokDubey
Created February 15, 2018 15:55
Show Gist options
  • Save AbhishekAshokDubey/110ebae9c48e4f5604c0feef13121cc2 to your computer and use it in GitHub Desktop.
Save AbhishekAshokDubey/110ebae9c48e4f5604c0feef13121cc2 to your computer and use it in GitHub Desktop.
using Microsoft.Rtc.Collaboration;
using Microsoft.Rtc.Signaling;
using System;
using System.Threading;
namespace ucma1
{
class Program
{
private const string sipaddress = "sip:sender@domain.com";
private const string username = "sender@domain.com";
private const string password = "senders_password";
private const string domain = "domain";
private const string destinationSip = "sip:receiver@sender.com"; // port: 5061
private const string IMMessage = "Hello! from the bot, just ignore this";
private static String _conversationPriority = ConversationPriority.Urgent;
private static String _conversationSubject = "The AskHR Bot!";
private static InstantMessagingFlow _instantMessagingFlow;
//private InstantMessagingFlow _instantMessagingFlow;
static CollaborationPlatform _collabPlatform { get; set; }
static UserEndpoint _endpoint { get; set; }
static bool _OKToQuit = false;
static void Main(string[] args)
{
string userAgent = "ClientPlatformExample";
var platformSettings = new ClientPlatformSettings(userAgent, Microsoft.Rtc.Signaling.SipTransportType.Tls);
_collabPlatform = new CollaborationPlatform(platformSettings);
//Start up the platform, calling back asynchronously once it's done.
_collabPlatform.BeginStartup(EndCollabPlatformStartup, null);
//In this example, wait for everything to finish before exiting
while (!_OKToQuit)
{
System.Threading.Thread.Sleep(2000);
}
}
private static void EndCollabPlatformStartup(IAsyncResult ar)
{
_collabPlatform.EndStartup(ar);
//A collaboration plaform can have one or more Endpoints. An Endpoint is tied to a SIP Address.
UserEndpointSettings settings = new UserEndpointSettings(sipaddress);
settings.Credential = new System.Net.NetworkCredential(username, password, domain);
settings.AutomaticPresencePublicationEnabled = true;
try {
_endpoint = new UserEndpoint(_collabPlatform, settings);
_endpoint.RegisterForIncomingCall<InstantMessagingCall>(OnIncomingCall);
_endpoint.BeginEstablish(UserEndpointEstablishCompleted, _endpoint);
}
catch(InvalidOperationException) {
Console.WriteLine("Could not establish endpoint. Platform was not in a valid state.");
}
}
private static void OnIncomingCall(object sender, CallReceivedEventArgs<InstantMessagingCall> e) {
try
{
Console.WriteLine(e.RemoteParticipant.Uri);
Console.WriteLine(e.ToastMessage.Message);
}
catch {
Console.WriteLine("e.RemoteParticipant.Uri");
}
}
private static void UserEndpointEstablishCompleted(IAsyncResult ar)
{
try {
//LocalEndpoint currentEndpoint = ar.AsyncState as LocalEndpoint;
//currentEndpoint.EndEstablish(ar);
_endpoint.EndEstablish(ar);
}
catch (RealTimeException exception) {
Console.WriteLine("Failed to establish endpoint: {0}", exception.Message);
}
// Setup the conversation and place the call.
ConversationSettings convSettings = new ConversationSettings();
convSettings.Priority = _conversationPriority;
convSettings.Subject = _conversationSubject;
//Once the endpoint is in place, create a Conversation and an IM Call.
var Conversation = new Conversation(_endpoint, convSettings);
var Call = new InstantMessagingCall(Conversation);
Call.StateChanged += InstantMessagingCall_StateChanged;
//When the call is established, Flow will be created. Flow is how you sent IMs around. Therefore, just before
//establishing, we attach an event handler to catch the flow being setup (it's state will change to Active)
Call.InstantMessagingFlowConfigurationRequested += Call_InstantMessagingFlowConfigurationRequested;
Call.BeginEstablish(destinationSip, new CallEstablishOptions(), EndBeginEstablish, Call);
}
// Just to record the state transitions in the console.
static void InstantMessagingCall_StateChanged(object sender, CallStateChangedEventArgs e)
{
Console.WriteLine("Call has changed state. The previous call state was: " + e.PreviousState +
"and the current state is: " + e.State);
}
private static void EndBeginEstablish(IAsyncResult ar)
{
Call call = (Call)ar.AsyncState;
call.EndEstablish(ar);
}
static void Call_InstantMessagingFlowConfigurationRequested(object sender, InstantMessagingFlowConfigurationRequestedEventArgs e)
{
//InstantMessagingFlow _instantMessagingFlow = e.Flow;
_instantMessagingFlow = e.Flow;
//Once we're notified about this, we get a handle to the newly created Flow.
//Let's use this to register for state changes.
_instantMessagingFlow.StateChanged += Flow_StateChanged;
// Message Received is the event used to indicate that a message has
// been received from the far end.
_instantMessagingFlow.MessageReceived += Flow_MessageReceived;
// typing
_instantMessagingFlow.RemoteComposingStateChanged +=
InstantMessagingFlow_RemoteComposingStateChanged;
}
static void InstantMessagingFlow_RemoteComposingStateChanged(object sender,
ComposingStateChangedEventArgs e)
{
// Prints the typing notifications of the far end user.
Console.WriteLine("Participant "
+ e.Participant.Uri.ToString()
+ " is "
+ e.ComposingState.ToString()
);
}
static void Flow_MessageReceived(object sender, InstantMessageReceivedEventArgs e) {
Console.WriteLine(e.Sender.Uri + " said: " + e.TextBody);
_instantMessagingFlow.LocalComposingState = ComposingState.Composing;
Thread.Sleep(2000);
if (e.TextBody.ToLower().Contains("bye"))
{
_instantMessagingFlow.BeginSendInstantMessage("Echo: " + e.TextBody, EndBeginSendInstanceMessage, _instantMessagingFlow);
}
else {
_instantMessagingFlow.BeginSendInstantMessage("Echo: " + e.TextBody, GoOn, _instantMessagingFlow);
}
}
static void GoOn(IAsyncResult ar) {
_instantMessagingFlow.LocalComposingState = ComposingState.Idle;
}
static void Flow_StateChanged(object sender, MediaFlowStateChangedEventArgs e)
{
if (e.State == MediaFlowState.Active)
{
//The flow is now active! We can use it to send messages.
//InstantMessagingFlow flow = (InstantMessagingFlow)sender;
//flow.BeginSendInstantMessage(IMMessage, EndBeginSendInstanceMessage, flow);
_instantMessagingFlow.BeginSendInstantMessage(IMMessage, GoOn, _instantMessagingFlow);
}
}
private static void EndBeginSendInstanceMessage(IAsyncResult ar)
{
_instantMessagingFlow.LocalComposingState = ComposingState.Idle;
InstantMessagingFlow flow = (InstantMessagingFlow)ar.AsyncState;
flow.EndSendInstantMessage(ar);
//Having sent the message, terminate the conversation
flow.Call.Conversation.BeginTerminate(EndBeginTerminate, flow.Call.Conversation);
}
private static void EndBeginTerminate(IAsyncResult ar)
{
Conversation conversation = (Conversation)ar.AsyncState;
conversation.EndTerminate(ar);
//_OKToQuit = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment