Skip to content

Commit

Permalink
Adding Skype sample from documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
RobStand committed Apr 21, 2017
1 parent ba5ed44 commit 452a9b0
Show file tree
Hide file tree
Showing 20 changed files with 1,612 additions and 0 deletions.
56 changes: 56 additions & 0 deletions CSharp/skype-CallingBot/AgentListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace EmergencyServicesBot
{
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;

public class AgentListener
{
// This will send an adhoc message to the user
public static async Task Resume(
string toId,
string toName,
string fromId,
string fromName,
string conversationId,
string message,
string serviceUrl = "https://smba.trafficmanager.net/apis/",
string channelId = "skype")
{
if (!MicrosoftAppCredentials.IsTrustedServiceUrl(serviceUrl))
{
MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);
}

try
{
var userAccount = new ChannelAccount(toId, toName);
var botAccount = new ChannelAccount(fromId, fromName);
var connector = new ConnectorClient(new Uri(serviceUrl));

IMessageActivity activity = Activity.CreateMessageActivity();

if (!string.IsNullOrEmpty(conversationId) && !string.IsNullOrEmpty(channelId))
{
activity.ChannelId = channelId;
}
else
{
conversationId = (await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount)).Id;
}

activity.From = botAccount;
activity.Recipient = userAccount;
activity.Conversation = new ConversationAccount(id: conversationId);
activity.Text = message;
activity.Locale = "en-Us";
await connector.Conversations.SendToConversationAsync((Activity)activity);
}
catch (Exception exp)
{
Debug.WriteLine(exp);
}
}
}
}
33 changes: 33 additions & 0 deletions CSharp/skype-CallingBot/App_Start/WebApiConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace EmergencyServicesBot
{
using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Json settings
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Newtonsoft.Json.Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
};

// Web API configuration and services

// Web API routes
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
}
}
}
30 changes: 30 additions & 0 deletions CSharp/skype-CallingBot/Controllers/CallController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace EmergencyServicesBot
{
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Bot.Builder.Calling;
using Microsoft.Bot.Connector;

[BotAuthentication]
[RoutePrefix("api/calling")]
public class CallingController : ApiController
{
public CallingController() : base()
{
CallingConversation.RegisterCallingBot(callingBotService => new IVRBot(callingBotService));
}

[Route("callback")]
public async Task<HttpResponseMessage> ProcessCallingEventAsync()
{
return await CallingConversation.SendAsync(this.Request, CallRequestType.CallingEvent);
}

[Route("call")]
public async Task<HttpResponseMessage> ProcessIncomingCallAsync()
{
return await CallingConversation.SendAsync(this.Request, CallRequestType.IncomingCall);
}
}
}
68 changes: 68 additions & 0 deletions CSharp/skype-CallingBot/Controllers/MessagesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace EmergencyServicesBot
{
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Bot.Connector;

[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

// calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;

// return our reply to the user
Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
await connector.Conversations.ReplyToActivityAsync(reply);
}
else
{
this.HandleSystemMessage(activity);
}

var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}

private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing tha the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}

return null;
}
}
}
Loading

0 comments on commit 452a9b0

Please sign in to comment.