Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Microsoft/BotBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevenic committed Apr 8, 2016
2 parents d19d228 + 1df92d2 commit 162c33d
Show file tree
Hide file tree
Showing 34 changed files with 1,619 additions and 1 deletion.
131 changes: 131 additions & 0 deletions CSharp/Samples/Build-2016/Stock-LUIS/Model/Stock Model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"luis_schema_version": "1.2.0",
"name": "Stock Model",
"desc": "",
"culture": "en-us",
"intents": [
{
"Name": "RepeatLastStock",
"Children": []
},
{
"Name": "StockPrice",
"Children": []
},
{
"Name": "IsTheMarketUpOrDown",
"Children": []
},
{
"Name": "None",
"Children": []
}
],
"entities": [
{
"Name": "TickerSymbol",
"Children": []
}
],
"bing_entities": [],
"actions": [],
"model_features": [],
"regex_features": [],
"utterances": [
{
"text": "is the market up or down today?",
"intent": "IsTheMarketUpOrDown",
"entities": []
},
{
"text": "how is the market doing today?",
"intent": "IsTheMarketUpOrDown",
"entities": []
},
{
"text": "how's the market today?",
"intent": "IsTheMarketUpOrDown",
"entities": []
},
{
"text": "tell me the price of tsla",
"intent": "StockPrice",
"entities": [
{
"entity": "TickerSymbol",
"startPos": 5,
"endPos": 5
}
]
},
{
"text": "what's the price of tsla?",
"intent": "StockPrice",
"entities": [
{
"entity": "TickerSymbol",
"startPos": 6,
"endPos": 6
}
]
},
{
"text": "what's the market doing?",
"intent": "IsTheMarketUpOrDown",
"entities": []
},
{
"text": "what's going on in the market?",
"intent": "IsTheMarketUpOrDown",
"entities": []
},
{
"text": "show me the price for msft",
"intent": "StockPrice",
"entities": [
{
"entity": "TickerSymbol",
"startPos": 5,
"endPos": 5
}
]
},
{
"text": "what is aapl at today",
"intent": "StockPrice",
"entities": [
{
"entity": "TickerSymbol",
"startPos": 2,
"endPos": 2
}
]
},
{
"text": "show me yhoo",
"intent": "None",
"entities": [
{
"entity": "TickerSymbol",
"startPos": 2,
"endPos": 2
}
]
},
{
"text": "how about now",
"intent": "RepeatLastStock",
"entities": []
},
{
"text": "and now?",
"intent": "RepeatLastStock",
"entities": []
},
{
"text": "repeat last stock",
"intent": "RepeatLastStock",
"entities": []
}
]
}
22 changes: 22 additions & 0 deletions CSharp/Samples/Build-2016/Stock-LUIS/Stock-LUIS.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stock-LUIS", "Stock-LUIS\Stock-LUIS.csproj", "{A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace Stock_LUIS
{
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 }
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Utilities;
using Newtonsoft.Json;

namespace Stock_LUIS
{
[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<Message> Post([FromBody]Message message)
{
if (message.Type == "Message")
{
bool bSetStock = false;
StockLUIS stLuis = await LUISStockClient.ParseUserInput(message.Text);
string strRet = string.Empty;
string strStock = message.Text;

if (stLuis.intents.Count() > 0)
{
switch (stLuis.intents[0].intent)
{
case "RepeatLastStock":
strStock = message.GetBotUserData<string>("LastStock");
if (null == strStock)
{
strRet = "I don't have a previous stock to look up!";
}
else
{
strRet = await GetStock(strStock);
}
break;
case "StockPrice":
bSetStock = true;
strRet = await GetStock(stLuis.entities[0].entity);
break;
default:
break;
}
}
else
{
strRet = "Sorry, I don't understand...";
}


Message ReplyMessage = message.CreateReplyMessage(strRet);
if (bSetStock)
{
ReplyMessage.SetBotUserData("LastStock", stLuis.entities[0].entity);
}
return ReplyMessage;
}
else
{
return HandleSystemMessage(message);
}
}

private async Task<string> GetStock(string strStock)
{
string strRet = string.Empty;
double? dblStock = await Yahoo.GetStockPriceAsync(strStock);
// return our reply to the user
if (null == dblStock)
{
strRet = string.Format("Stock {0} doesn't appear to be valid", strStock.ToUpper());
}
else
{
strRet = string.Format("Stock: {0}, Value: {1}", strStock.ToUpper(), dblStock);
}

return strRet;
}

private Message HandleSystemMessage(Message message)
{
if (message.Type == "Ping")
{
Message reply = message.CreateReplyMessage();
reply.Type = "Ping";
return reply;
}
else if (message.Type == "DeleteUserData")
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == "BotAddedToConversation")
{
}
else if (message.Type == "BotRemovedFromConversation")
{
}
else if (message.Type == "UserAddedToConversation")
{
}
else if (message.Type == "UserRemovedFromConversation")
{
}
else if (message.Type == "EndOfConversation")
{
}

return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%@ Application Codebehind="Global.asax.cs" Inherits="Stock_LUIS.WebApiApplication" Language="C#" %>
17 changes: 17 additions & 0 deletions CSharp/Samples/Build-2016/Stock-LUIS/Stock-LUIS/Global.asax.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing;

namespace Stock_LUIS
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
}
55 changes: 55 additions & 0 deletions CSharp/Samples/Build-2016/Stock-LUIS/Stock-LUIS/Luis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Stock_LUIS
{
public class LUISStockClient
{
public static async Task<StockLUIS> ParseUserInput(string strInput)
{
string strRet = string.Empty;
string strEscaped = Uri.EscapeDataString(strInput);

using (var client = new HttpClient())
{
string uri = "https://api.projectoxford.ai/luis/v1/application?id=2f5aa73e-244c-4a4b-931d-44331ab5b1eb&subscription-key=6d0966209c6e4f6b835ce34492f3e6d9&q=" + strEscaped;
HttpResponseMessage msg = await client.GetAsync(uri);

if (msg.IsSuccessStatusCode)
{
var jsonResponse = await msg.Content.ReadAsStringAsync();
var _Data = JsonConvert.DeserializeObject<StockLUIS>(jsonResponse);
return _Data;
}
}
return null;
}
}

public class StockLUIS
{
public string query { get; set; }
public lIntent[] intents { get; set; }
public lEntity[] entities { get; set; }
}

public class lIntent
{
public string intent { get; set; }
public float score { get; set; }
}

public class lEntity
{
public string entity { get; set; }
public string type { get; set; }
public int startIndex { get; set; }
public int endIndex { get; set; }
public float score { get; set; }
}
}
Loading

0 comments on commit 162c33d

Please sign in to comment.