Skip to content

Instantly share code, notes, and snippets.

@burtonian
Created October 15, 2011 00:01
Show Gist options
  • Save burtonian/1288720 to your computer and use it in GitHub Desktop.
Save burtonian/1288720 to your computer and use it in GitHub Desktop.
Emancipay ListenLog Prototype
ruleset a35x110 {
meta {
name "VRM EmanciPay"
description <<
VRM EmanciPay prototype with ListenLog
Twilio # Your Twilio number
>>
author "Craig Burton with a lot of help from Ed Orcutt"
logging off
key twitter {
"consumer_key" : "dEq8xCHLGzYONdwiVUfOlw",
"consumer_secret" : "your Twitter consumer goes secret here",
"oauth_token" : "3339171-4QfJ8BxzJsO57NPYMEXveCOWVawlnZ2by7HjlUpf1T",
"oauth_token_secret" : "Your Twitter Oauth token secret goes here"
}
}
dispatch {
// domain "example.com"
// we don't need no stinkin' dispatch
}
global {
pachubeputurl = "http://api.pachube.com/v2/feeds/37293/?key=(your pachube feed key goes here)"
//this is a setup for posting escrow to pachube.
dataset ListenLogArray:JSON
<-"http://listenlog.org/users/your ListenLog address goes here/export.json"
cachable for 1 minutes;
//this gets my current listenlog and puts it in the array called ListeLogArray
LogDetail = ListenLogArray.pick("$[-1:].doc")
//this parses the whole ListenLogArray and selects just the last thing I listened to
}
rule getContribution {
select when twilio emancipay Body "^[1-9][0-9]{0,1}$"
//this statement creates a webhook by the name of emancipay
//it also check to see if the regular expression is met
//the regex allows for 3 digits. The first digit cannot be a zero
pre{
amount = event:param("Body");
//assign the body of the sms message (an integer) to the var amount
}
{
twilio:sms("Doc, your contribution of $" + amount + ".00 was received.");
//acknowledge the amount by texting the string to the cell originating the text
}
}
rule determineType{
//Listenlog has two types of logs. Stream and Podcast.
//this rule figures out which one this event is and passes the values
//to the rule that then puts the amount and info in escrow
select when twilio emancipay Body "^[1-9][0-9]{0,1}$"
pre{
amount = event:param("Body");
messageType = LogDetail.pick("$..messageType");
programName = LogDetail.pick("$..program.title");
episodeName = LogDetail.pick("$..episode.title");
dateTime = LogDetail.pick("$..listenLog.createdAt");
duration = LogDetail.pick("$..listenLog.listenDuration");
displayName = LogDetail.pick("$..stream.displayName");
//assign var names to values in the array
//some of the var will be null depending on whether the record is a stream or podcast
}
if (messageType eq "logStream") then
//this is how to do an "if then else" evaluation with and action block
{
noop();
//no actions, just jump to the prelude and determin if then else
}
fired
{
raise explicit event postStream with title=title and
displayName=displayName and dateTime=dateTime and
duration = duration and
amount=amount;
//if the event is a stream call the rule postStream and pass these values to the rule
}else{
raise explicit event postPodcast with programName=programName and
episodeName=episodeName and
dateTime=dateTime
and duration = duration
and amount = amount;
//if the event is a podcast invoke the rule postPodcast and pass these values to the rule
}
}
rule postStream{
select when explicit postStream
pre{
amount = event:param("amount");
title = event:param("title");
displayName = event:param("displayName");
dateTime = event:param("dateTime");
duration = event:param("duration");
//assign the parameters passed in the explicit event to vars
radioStation = displayName.extract(re/([KW]\w\w\w)/).head();
//extract the call letters from the displayName
escrowBody = {
"version":"1.0.0",
"datastreams":[
{"id":"Contributions","current_value":amount}
]
};
}
{
http:put(pachubeputurl)
with headers = {"Content-Type":"application/json"}
and body = escrowBody
and response_headers = ["Status Code"];
//post amount in escrow at Pachube https://pachube.com/feeds/37293
twilio:sms ("Doc mah man, $"+amount+".00 escrowed for "+ radioStation + " " + title);
//send text to contributor about the amount and the program and station contributed to
twitter:update("test: I escrowed $"+amount+" for " + radioStation + " via #EmanciPay #ListenLog #VRM. http://bit.ly/hp4LBe");
//tweet the event
}
fired{
last;
//if this rule fires, stop here
}
}
rule postPodcast{
select when twilio emancipay and explicit postPodcast
pre{
amount = event:param("amount");
programName = event:param("programName");
episodeName = event:param("episodeName");
dateTime = event:param("dateTime");
duration = event:param("duration");
//assign the parameters passsed in the explicit event to vars
escrowBody = {
"version":"1.0.0",
"datastreams":[
{"id":"Contributions","current_value":amount}
]
};
}
{
http:put(pachubeputurl)
with headers = {"Content-Type":"application/json"}
and body = escrowBody
and response_headers = ["Status Code"];
//post amount in escrow at Pachube https://pachube.com/feeds/37293
twilio:sms ("Doc, dude, $"+amount+".00 escrowed for "+ programName + " " + episodeName);
//text to the contributor the amount and program station contributed to;
twitter:update("test: I escrowed $"+amount+" for " + programName + " via #EmanciPay #VRM #ListenLog. http://bit.ly/hp4LBe");
//tweet the event
}
fired{
last;
}
}
rule instruct is active{
select when twilio emancipay Body "^[0]|[\D]"
{
twilio:sms("Doc, whoah, please enter a dollar amount between 1-99 without $ or decimal point");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment