Created
November 19, 2013 11:31
-
-
Save lajunta/7544055 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#encoding: utf-8 | |
require "net/http" | |
require "json" | |
require "xmlsimple" | |
class YixinController < ApplicationController | |
layout nil | |
skip_before_filter :verify_authenticity_token | |
def index | |
get_index if request.get? | |
post_index if request.post? | |
end | |
def get_index | |
@ts=params['timestamp'] | |
@n=params['nonce'] | |
@s=params['signature'] | |
if is_yixin(@ts,@n,@s) | |
render :text=>params['echostr'] | |
end | |
end | |
def post_index | |
@ts=params['timestamp'] | |
@n=params['nonce'] | |
@s=params['signature'] | |
halt unless is_yixin(@ts,@n,@s) | |
infos=XmlSimple.xml_in(request.body) | |
@to_user,@from_user = infos['FromUserName'].first,infos['ToUserName'].first | |
case infos['MsgType'].first | |
when 'text' | |
content=infos['Content'].first | |
text_handler(content) | |
when 'event' | |
event_handler(infos) | |
when "image" | |
text_reply("This is a image") | |
else | |
text_reply("What do you say?") | |
end | |
end | |
private | |
def is_yixin(ts,n,s) | |
return false if ts.nil? | |
hsh={'token'=>'20090327','timestamp'=>ts,'nonce'=>n} | |
raw_string = hsh.values.sort.join() | |
raw_string.gsub!(/[^0-9]/, "") | |
signature = Digest::SHA1.hexdigest(raw_string) | |
s==signature ? true : false | |
end | |
def event_handler(src) | |
@event=src['Event'].first | |
@key=src['EventKey'].first | |
@pic_base="http://dl.sdedu.net/new_see/" | |
if @event=="subscribe" | |
subscribe_handler | |
elsif @event=="unsubscribe" | |
unsubscribe_handler | |
else | |
click_handler(@key) | |
end | |
end | |
def subscribe_handler | |
text_reply("hello my friend,welcome to dljy") | |
end | |
def unsubscribe_handler | |
text_reply("goodbye my friend,see you again") | |
end | |
def click_handler(key) | |
case key | |
when "festival_qa" | |
headers["Content-Type"]="text/xml" | |
render "yixin/festival_qa", :layout=>nil | |
when /news/ | |
cat=key.split("/")[1] | |
@articles=Article.checked.cat(cat,5) | |
headers["Content-Type"]="text/xml" | |
render "yixin/news",layout: nil | |
else | |
text_reply("I am not ready for you click") | |
end | |
end | |
def text_handler(key) | |
if key=="a" | |
text_reply("This is a") | |
elsif key=="help" | |
text_reply("这是帮助文本,不过我也帮不了你,我不还知道怎么帮助你。随便点点看看吧。") | |
else | |
text_reply("你说的是什么?我看不懂,回复help获得帮助。") | |
end | |
end | |
def text_reply(content) | |
timei=Time.now.to_i | |
#to_user,from_user = src['FromUserName'].first,src['ToUserName'].first | |
text_tpl="<xml> <ToUserName>#{@to_user}</ToUserName> <FromUserName>#{@from_user}</FromUserName> <CreateTime>#{timei}</CreateTime> <MsgType>text</MsgType> <Content>#{content}</Content> </xml> " | |
render :text=>text_tpl, :content_type=>"text/xml" | |
end | |
def news_reply | |
end | |
def music_reply | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment