Skip to content

Instantly share code, notes, and snippets.

@NickCraver
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NickCraver/e1054f904fd6e194123e to your computer and use it in GitHub Desktop.
Save NickCraver/e1054f904fd6e194123e to your computer and use it in GitHub Desktop.
IFTTT WordPress faker for listening to Amazon Echo - Empty OWIN App's Startup.cs
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-beta4",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
"Kestrel": "1.0.0-beta4"
},
"commands": {
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000",
"kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://0.0.0.0:5001"
},
"frameworks": {
"dnx451": {},
"dnxcore50": {}
},
"publishExclude": [
"node_modules",
"bower_components",
"**.xproj",
"**.user",
"**.vspscc"
],
"exclude": [
"wwwroot",
"node_modules",
"bower_components"
]
}
using System;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection;
namespace IFTTTEndpoint
{
public class Startup
{
public void ConfigureServices(IServiceCollection services) { }
static Regex methodRegex = new Regex(@"\<methodName\>(?<method>.*)\<\/methodName\>", RegexOptions.Singleline | RegexOptions.Compiled);
static Regex titleRegex = new Regex(@"<member><name>title</name><value><string>(?<title>.*?)</string></value></member>", RegexOptions.Singleline | RegexOptions.Compiled);
public void Configure(IApplicationBuilder app)
{
app.Run(async (context) =>
{
string body = new StreamReader(context.Request.Body).ReadToEnd();
var test = methodRegex.Match(body);
if (test.Success) {
switch(test.Groups["method"].Value) {
case "mt.supportedMethods":
await Xml(context, "metaWeblog.getRecentPosts");
break;
case "metaWeblog.getRecentPosts":
await Xml(context, "<array><data></data></array>");
break;
case "metaWeblog.newPost":
var title = titleRegex.Match(body);
Console.WriteLine("From Amazon: " + title.Groups["title"].Value);
await Xml(context, "<string>No forward url, but will assume data was handled locally</string>");
break;
}
} else {
Console.WriteLine("Unrecognized: " + body);
await context.Response.WriteAsync("Hello World!");
}
});
}
public Task Xml(HttpContext context, string content) {
context.Response.ContentType = "text/xml";
return context.Response.WriteAsync(string.Format(@"<?xml version=""1.0""?>
<methodResponse><params><param><value>{0}</value></param></params></methodResponse>", content));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment