wordpress上没有一个好的微信工具,于是这里就用上了 王超 写的 微信公众平台后台框架
这里是基于ruby上的sinatra框架,而wordpress方面则是依赖于wp api生成的json
这就是一个简单的示例
http://localhost/?wpapi=search&dev=1&keyword=title
会返回这样一个结果
{"status":"ok","count":2,"count_total":2,"pages":1,"currPage":1,"posts":[{"id":"4","type":"post","slug":"other-title","url":"http:\/\/xunta.phodal.net\/?p=4","status":"publish","title":"other title","title_plain":"other title","date":"2014-03-26 20:38:58","modified":"2014-03-26 20:38:58","excerpt":"this \u00a0a test","parent":"0","category":[{"term_id":1,"name":"Uncategorized","slug":"uncategorized","term_group":0,"term_taxonomy_id":1,"taxonomy":"category","description":"","parent":0,"count":2,"object_id":4,"filter":"raw","cat_ID":1,"category_count":2,"category_description":"","cat_name":"Uncategorized","category_nicename":"uncategorized","category_parent":0}],"tag":[],"author":[{"id":"1","slug":"root","name":"root","first_name":"","last_name":"","nickname":"root","url":"","description":"","gravatar":"http:\/\/www.gravatar.com\/avatar\/56d54d4b6b4dc4e3efa458f745d61513?s=100&d=mm&r=g"}],"comment_count":"0","comment_status":"open"}]}
而我们在ruby上所要做的就是解析这个json数据.
这里需要用到ruby的json库以及net/http
一个简单的http get请求
require 'json'
require 'net/http'
response = Net::HTTP.get_response("localhost","/?wpapi=search&dev=1&keyword=title")
p JSON.parse response.body
就可以解析完输出的结果
安装weChat
gem install 'wei-backend'
官方的示例如下
require 'sinatra'
require 'wei-backend'
token "mytoken"
on_text do
"你发送了如下内容: #{params[:Content]}!!"
end
on_subscribe do
"感谢您的订阅"
end
on_unsubscribe do
"欢迎您再次订阅"
end
所以我们需要布到服务器上面,同时修改一下上面的token
当然这里还支持下面这种格式
on_text do
[{
:title => '收到一个文本消息,返回两个图文消息',
:description => 'desc',
:picture_url => 'pic url',
:url => 'url'
},
{
:title => '这是第二个图文消息',
:description => 'desc1',
:picture_url => 'pic url1',
:url => 'url1'
}]
end
所以我们要做的就是把我们数据放进去
围观我的Github Idea墙, 也许,你会遇到心仪的项目