Created
October 22, 2011 07:43
-
-
Save crslade/1305748 to your computer and use it in GitHub Desktop.
Full code for Amazon FriendFeeback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruleset a680x5 { | |
meta { | |
name "ProductFriendFeedback" | |
description << | |
If you want to get feedback from a product in Facebook, this will give you a button to post the product your Facebook wall and ask a question. | |
>> | |
author "Chris Slade" | |
// Uncomment this line to require Marketplace purchase to use this app. | |
// authz require user | |
key facebook { | |
"consumer_key" : "REDACTED", | |
"consumer_secret" : "REDACTED" | |
} | |
logging off | |
} | |
dispatch { | |
domain "amazon.com" | |
} | |
global { | |
} | |
rule auth_app is active { | |
select using ".*" setting () | |
if (not facebook:authorized()) | |
then | |
facebook:authorize(["publish_stream","email","read_stream","offline_access","user_about_me","user_notes","friends_about_me"]) | |
with opacity = 1 and | |
sticky = true; | |
fired { | |
last; | |
} | |
} | |
//puts a button on the page to request for feedback. I need to handle types 2 urls from amazon. | |
rule place_button is active { | |
select when pageview ".*amazon.com/[\w\d-]+/dp/([\w\d]+)" setting (product_id) or | |
pageview ".*amazon.com/gp/product/([\w\d]+)/" setting (product_id) | |
pre { | |
item_url = page:env("caller"); | |
product_name = page:env("title").split(re/: /); | |
product_name = product_name[1]; | |
pid = product_id; | |
button_style = << | |
<style> | |
<br/> #cFeedbackSubmitButton {<br/> background-color: #3B5998;<br/> color: #FFF; <br/> } <br/> | |
</style> | |
>>; | |
facebook_div = << | |
<div id="friend_feedback"> | |
<br /> | |
<h2> | |
Friend Feedback</h2> | |
<br /> | |
<div> | |
<br /> | |
<form id="cFeedbackButton" onsubmit="false"> | |
<br /> | |
<input name="productName" type="hidden" value="#{product_name}" /><br /> | |
<input name="productId" type="hidden" value="#{product_id}" /><br /> | |
<input name="productUrl" type="hidden" value="#{item_url}" /> <br /> | |
<input id="cFeedbackSubmitButton" type="submit" value="Get Friend Feedback" /></form> | |
</div> | |
<br /> | |
<div id="cpostFeedback"> | |
</div> | |
</div> | |
>>; | |
} | |
{ | |
append("head",button_style); | |
before("#customerReviews", facebook_div); | |
watch("#cFeedbackButton","submit"); | |
} | |
} | |
//This loops through the history in (ent:requested_products) seeing if the current product has been requested. | |
rule need_discussion is active { | |
select when pageview ".*amazon.com/[\w\d-]+/dp/([\w\d]+)" setting (product_id) or | |
pageview ".*amazon.com/gp/product/([\w\d]+)/" setting (product_id) | |
foreach ent:requested_products.as("array") setting (hist_id) | |
pre { | |
not_text = << | |
#{hist_id} #{product_id} | |
>>; | |
split_hist_id = hist_id.split(re/:/); | |
prod_id = split_hist_id[0]; | |
post_id = split_hist_id[1]; | |
} | |
if product_id eq prod_id then { | |
noop(); | |
} | |
fired { | |
raise explicit event place_discussion with postId = post_id | |
} | |
} | |
//This is fired when this page has a facebook post in the history. | |
rule place_discussion is active { | |
select when explicit place_discussion | |
pre { | |
post_id = page:param("postId"); | |
post_data = facebook:get({"id": post_id}); | |
likes = facebook:get({"id" : post_id, "connection" : "likes" }); | |
likes_count = likes.pick("$.data").length(); | |
likes_count_text = (likes_count == 1) => "1 friend likes this item" | "#{likes_count} friends like this item."; | |
comments = facebook:get({"id" : post_id, "connection" : "comments"}).pick("$.data"); | |
comments_count = comments.length(); | |
//comments_count = post_data.pick("$.comments.count"); | |
comments_count_text = (comments_count == 1) => "1 comment." | "#{comments_count} comments."; | |
fb_post_url = post_data.pick("$.actions[0].link"); | |
post_text = << | |
<div id="cfbNotes"> | |
<br /> | |
<h3> | |
Feedback</h3> | |
<br /> | |
#{likes_count_text}<br /> | |
<a href="http://www.blogger.com/blogger.g?blogID=2721499664417059501#{fb_post_url}">#{comments_count_text}</a><br /> | |
</div> | |
>>; | |
comment_style = << | |
<style> | |
<br/> .cfacebookComment {<br/> padding: 5px;<br/> margin-top: 2px;<br/> background-color: #EDEFF4;<br/> border-bottom: 1px solid #E5EAF1;<br/> width: 35%; <br/> }<br/> .cfacebookComment span {<br/> color: #3B5998;<br/> font-weight: bold; <br/> }<br/> | |
</style> | |
>>; | |
} | |
{ | |
append("head",comment_style); | |
replace_inner("#cpostFeedback",post_text); | |
} | |
fired { | |
raise explicit event place_comments with postId = post_id; | |
} | |
} | |
//This will loop through all of the comments and place them on the page. | |
rule place_comments is active { | |
select when explicit place_comments | |
foreach facebook:get({"id" : page:param("postId"), "connection" : "comments"}).pick("$.data") setting (cmt) | |
pre { | |
from_name = cmt.pick("$.from.name"); | |
message = cmt.pick("$.message"); | |
comment_text = << | |
<div class="cfacebookComment"> | |
<br /> | |
#{from_name} #{message} <br /> | |
<div> | |
<br /> | |
>>; <br /> | |
}<br /> | |
{ <br /> | |
append("#cpostFeedback",comment_text);<br /> | |
} <br /> | |
} <br /> | |
<br /> | |
<br /> | |
rule launch_form is active {<br /> | |
select when web submit "#cFeedbackButton"<br /> | |
pre {<br /> | |
product_name = page:param("productName");<br /> | |
product_id = page:param("productId");<br /> | |
product_url = page:param("productUrl"); <br /> | |
form_style = <<<br /> | |
<style> | |
<br/> #cFeedbackTextBox {<br/> width: 350px<br/> } <br/> | |
</style> <br /> | |
>>;<br /> | |
<br /> | |
feedback_form = <<<br /> | |
<div id="cEnterFeedback"> | |
<br /> | |
<h3> | |
#{product_name}</h3> | |
<br /> | |
<form id="cFeedbackForm" onsubmit="false"> | |
<br /> | |
<textarea id="cFeedbackTextBox" name="feedBackText">I am thinking about buying a #{product_name}. What do you think?</textarea><br /> | |
<input name="productId" type="hidden" value="#{product_id}" /><br /> | |
<input name="productName" type="hidden" value="#{product_name}" /><br /> | |
<input name="productUrl" type="hidden" value="#{product_url}" /> <br /> | |
<input type="submit" value="Post to Facebook" /> </form> | |
</div> | |
<br /> | |
>>; <br /> | |
}<br /> | |
{ <br /> | |
append("head",form_style);<br /> | |
notify("Request Feedback from your Friends",feedback_form) with<br /> | |
width = 400 and sticky = true and position = "top-left" and background_color = "#3B5998";<br /> | |
watch("#cFeedbackForm", "submit"); <br /> | |
} <br /> | |
}<br /> | |
<br /> | |
rule post_request is active {<br /> | |
select when web submit "#cFeedbackForm"<br /> | |
pre {<br /> | |
product_id = page:param("productId"); <br /> | |
product_url = page:param("productUrl");<br /> | |
post_text = page:param("feedBackText");<br /> | |
<br /> | |
mdata = facebook:metadata();<br /> | |
fid = mdata.pick("$.id");<br /> | |
fname = mdata.pick("$.first_name");<br /> | |
notify_text = <<<br /> | |
Posted to #{fname}'s Facebook wall.<br /> | |
<br /> | |
>>;<br /> | |
}<br /> | |
{<br /> | |
replace_html("#cEnterFeedback",notify_text);<br /> | |
facebook:post({"id" : fid,<br /> | |
"connection" : "feed",<br /> | |
"message" : post_text,<br /> | |
"link" : product_url,<br /> | |
"description" : "Posted by Product Friend Feedback"}); <br /> | |
}<br /> | |
fired {<br /> | |
raise explicit event recent_post with productId = product_id and productUrl = product_url;<br /> | |
} <br /> | |
}<br /> | |
<br /> | |
//since I cannot get the post id when I post it, I have to request the feeds from the wall and get the first one and hope that that was what I just posted. <br /> | |
rule recent_post is active {<br /> | |
select when explicit recent_post<br /> | |
pre {<br /> | |
product_id = page:param("productId");<br /> | |
product_url = page:param("productUrl");<br /> | |
mdata = facebook:metadata();//slows down the next get to try to make sure post is updated. <br /> | |
result = facebook:get({"connection" : "feed"}); //same<br /> | |
result = facebook:get({"connection" : "feed"});<br /> | |
post_id = result.pick("$..data[0].id");<br /> | |
//sometimes I get an array and sometimes I don't. I don't know why facebook isn't consistant.<br /> | |
//This sees if I get an array, and gets just the first one. <br /> | |
post_id = (post_id.as("str").match(re/^[\d]+_[\d]+$/)) => post_id | post_id[0]; <br /> | |
hist_text = product_id+":"+post_id;<br /> | |
}<br /> | |
{<br /> | |
noop(); <br /> | |
}<br /> | |
fired {<br /> | |
mark ent:requested_products with hist_text<br /> | |
}<br /> | |
}<br /> | |
<br /> | |
}</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment