Skip to content

Commit 452a9b0

Browse files
committed
Adding Skype sample from documentation
1 parent ba5ed44 commit 452a9b0

20 files changed

+1612
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace EmergencyServicesBot
2+
{
3+
using System;
4+
using System.Diagnostics;
5+
using System.Threading.Tasks;
6+
using Microsoft.Bot.Connector;
7+
8+
public class AgentListener
9+
{
10+
// This will send an adhoc message to the user
11+
public static async Task Resume(
12+
string toId,
13+
string toName,
14+
string fromId,
15+
string fromName,
16+
string conversationId,
17+
string message,
18+
string serviceUrl = "https://smba.trafficmanager.net/apis/",
19+
string channelId = "skype")
20+
{
21+
if (!MicrosoftAppCredentials.IsTrustedServiceUrl(serviceUrl))
22+
{
23+
MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);
24+
}
25+
26+
try
27+
{
28+
var userAccount = new ChannelAccount(toId, toName);
29+
var botAccount = new ChannelAccount(fromId, fromName);
30+
var connector = new ConnectorClient(new Uri(serviceUrl));
31+
32+
IMessageActivity activity = Activity.CreateMessageActivity();
33+
34+
if (!string.IsNullOrEmpty(conversationId) && !string.IsNullOrEmpty(channelId))
35+
{
36+
activity.ChannelId = channelId;
37+
}
38+
else
39+
{
40+
conversationId = (await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount)).Id;
41+
}
42+
43+
activity.From = botAccount;
44+
activity.Recipient = userAccount;
45+
activity.Conversation = new ConversationAccount(id: conversationId);
46+
activity.Text = message;
47+
activity.Locale = "en-Us";
48+
await connector.Conversations.SendToConversationAsync((Activity)activity);
49+
}
50+
catch (Exception exp)
51+
{
52+
Debug.WriteLine(exp);
53+
}
54+
}
55+
}
56+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace EmergencyServicesBot
2+
{
3+
using System.Web.Http;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Serialization;
6+
7+
public static class WebApiConfig
8+
{
9+
public static void Register(HttpConfiguration config)
10+
{
11+
// Json settings
12+
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
13+
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
14+
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
15+
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
16+
{
17+
ContractResolver = new CamelCasePropertyNamesContractResolver(),
18+
Formatting = Newtonsoft.Json.Formatting.Indented,
19+
NullValueHandling = NullValueHandling.Ignore,
20+
};
21+
22+
// Web API configuration and services
23+
24+
// Web API routes
25+
config.MapHttpAttributeRoutes();
26+
27+
config.Routes.MapHttpRoute(
28+
name: "DefaultApi",
29+
routeTemplate: "api/{controller}/{id}",
30+
defaults: new { id = RouteParameter.Optional });
31+
}
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace EmergencyServicesBot
2+
{
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using System.Web.Http;
6+
using Microsoft.Bot.Builder.Calling;
7+
using Microsoft.Bot.Connector;
8+
9+
[BotAuthentication]
10+
[RoutePrefix("api/calling")]
11+
public class CallingController : ApiController
12+
{
13+
public CallingController() : base()
14+
{
15+
CallingConversation.RegisterCallingBot(callingBotService => new IVRBot(callingBotService));
16+
}
17+
18+
[Route("callback")]
19+
public async Task<HttpResponseMessage> ProcessCallingEventAsync()
20+
{
21+
return await CallingConversation.SendAsync(this.Request, CallRequestType.CallingEvent);
22+
}
23+
24+
[Route("call")]
25+
public async Task<HttpResponseMessage> ProcessIncomingCallAsync()
26+
{
27+
return await CallingConversation.SendAsync(this.Request, CallRequestType.IncomingCall);
28+
}
29+
}
30+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
namespace EmergencyServicesBot
2+
{
3+
using System;
4+
using System.Net;
5+
using System.Net.Http;
6+
using System.Threading.Tasks;
7+
using System.Web.Http;
8+
using Microsoft.Bot.Connector;
9+
10+
[BotAuthentication]
11+
public class MessagesController : ApiController
12+
{
13+
/// <summary>
14+
/// POST: api/Messages
15+
/// Receive a message from a user and reply to it
16+
/// </summary>
17+
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
18+
{
19+
if (activity.Type == ActivityTypes.Message)
20+
{
21+
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
22+
23+
// calculate something for us to return
24+
int length = (activity.Text ?? string.Empty).Length;
25+
26+
// return our reply to the user
27+
Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
28+
await connector.Conversations.ReplyToActivityAsync(reply);
29+
}
30+
else
31+
{
32+
this.HandleSystemMessage(activity);
33+
}
34+
35+
var response = Request.CreateResponse(HttpStatusCode.OK);
36+
return response;
37+
}
38+
39+
private Activity HandleSystemMessage(Activity message)
40+
{
41+
if (message.Type == ActivityTypes.DeleteUserData)
42+
{
43+
// Implement user deletion here
44+
// If we handle user deletion, return a real message
45+
}
46+
else if (message.Type == ActivityTypes.ConversationUpdate)
47+
{
48+
// Handle conversation state changes, like members being added and removed
49+
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
50+
// Not available in all channels
51+
}
52+
else if (message.Type == ActivityTypes.ContactRelationUpdate)
53+
{
54+
// Handle add/remove from contact lists
55+
// Activity.From + Activity.Action represent what happened
56+
}
57+
else if (message.Type == ActivityTypes.Typing)
58+
{
59+
// Handle knowing tha the user is typing
60+
}
61+
else if (message.Type == ActivityTypes.Ping)
62+
{
63+
}
64+
65+
return null;
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)