0.0
No commit activity in last 3 years
No release in over 3 years
Provide extensible Sinatra API to support rapid Wechat development
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Sinatra Wechat extension

Build Status Gem Version Coverage Status

This extension is used to support Tencent Wechat rapid development.

Installation

$ gem install sinatra-wechat

Usage

Below code implement a simple wechat robot, reply text 你好 when message sent by end user contains number %r{\d+}.

use :validate_msg => false to disable wechat message validation, otherwise need to append signature to the URL. The default value of :validate_msg is true

# app.rb
require 'sinatra'
require 'sinatra/wechat'

wechat('/', :wechat_token => 'test-token', :validate_msg => false) {
  text(:content => %r{\d+}) {
  	content_type 'application/xml'
  	erb :hello, :locals => request[:wechat_values]
  }
}

__END__
@@ hello
<xml>
  <ToUserName><%= from_user_name %></ToUserName>
  <FromUserName><%= to_user_name %></FromUserName>
  <CreateTime><%= Time.now.to_i %></CreateTime>
  <MsgType>text</MsgType>
  <Content><![CDATA[你好]]></Content>
</xml>

start web server:

$ ruby app.rb

validate it via cURL:

$ curl -X POST --data '<xml>
<ToUserName>tousername</ToUserName>
<FromUserName>fromusername</FromUserName> 
<CreateTime>1348831860</CreateTime>
<MsgType>text</MsgType>
<Content>1345 match number</Content>
<MsgId>1234567890123456</MsgId>
</xml>' http://localhost:4567

The response is like:

<xml>
<ToUserName>fromusername</ToUserName>
<FromUserName>tousername</FromUserName>
<CreateTime>1405790652</CreateTime>
<MsgType>text</MsgType>
<Content><![CDATA[你好]]></Content>
</xml>