• Stars
    star
    105
  • Rank 326,271 (Top 7 %)
  • Language
    Ruby
  • Created over 11 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

微信公众平台 开放消息接口 Rack Middleware

微信公众平台 开放消息接口 Rack Middleware

Build Status Gem Version

  • 验证微信请求 with 'weixin/middleware'
  • 解析推送消息 with 'weixin/model'
  • 生成回复消息 with 'weixin/model'

Installation

$ gem install rack-weixin

Usage

A sinatra demo

# -*- encoding : utf-8 -*-
require 'sinatra'
require 'rack-weixin'

use Weixin::Middleware, 'your api token', '/your_app_root' 

configure do
    set :wx_id, 'your_weixin_account'
end

helpers do
  def msg_router(msg)
    case msg.MsgType
    when 'text'
      # text message handler
    when 'image'
      # image message handler
    when 'location'
      # location message handler
    when 'link'
      # link message handler
    when 'event'
      # event messge handler
    when 'voice'
      # voice message handler
    when 'video'
      # video message handler
    else
      Weixin.text_msg(msg.ToUserName, msg.FromUserName, '未知消息类型')
    end
  end
end

get '/your_app_root' do
    params[:echostr]
end

post '/your_app_root' do
    content_type :xml, 'charset' => 'utf-8'

    message = request.env[Weixin::Middleware::WEIXIN_MSG]
    logger.info "原始数据: #{request.env[Weixin::Middleware::WEIXIN_MSG_RAW]}"
    
    # handle the message according to your business logic
    msg_router(message) unless message.nil?
end

Padrino下使用

Gemfile里加入:

gem 'rack-weixin'

config/apps.rb关闭以下两个:

set :protection, false
set :protect_from_csrf, false

app.rb里加入:

use Weixin::Middleware, 'your api token', '/your_app_root' 

configure do
	  set :wx_id, 'your_weixin_account'
end

Rack和Rails ActionController配合下使用

class WeixinController < ActionController::Base

  def index
    params[:echostr]
  end

  def create

    xml_message = request.env[Weixin::Middleware::WEIXIN_MSG]

    raw_message = request.env[Weixin::Middleware::WEIXIN_MSG_RAW]

    Rails.logger.debug "raw_message = " do
      raw_message
    end

    if xml_message.present?
      response_xml = msg_router(xml_message)

      Rails.logger.info "Weixin response_xml = " do
        response_xml
      end

    end

    if response_xml.present?
      render xml: response_xml
    else
      render :nothing => true, :status => 200, :content_type => 'text/html'
    end

  end

  def msg_router(msg)
    case msg.MsgType
      when 'text'
        text_parse(msg)
      when 'image'
        image_parse(msg)
      when 'location'
        location_parse(msg)
      when 'link'
        link_parse(msg)
      when 'event'
        event_parse(msg)
      when 'voice'
        voice_parse(msg)
      when 'video'
        video_parse(msg)
      else
    end

  end

end

Rails before_filter验证消息,用于接入多个微信公众号

class WeixinController < ActionController::Base

  before_filter :check_signature

  def index
    render :text => params[:echostr]
  end

  def create

    raw_msg = env[Weixin::Middleware::POST_BODY].read

    begin

       xml_message = Weixin::Message.factory(raw_msg)

    rescue Exception => e
      message = "weixin post message error!"
      Rails.logger.error "#{message} params = " do
        params
      end
    end
    
    Rails.logger.debug "raw_message = " do
      raw_message
    end

    if xml_message.present?
      response_xml = msg_router(xml_message)

      Rails.logger.info "Weixin response_xml = " do
        response_xml
      end

    end

    if response_xml.present?
      render xml: response_xml
    else
      render :nothing => true, :status => 200, :content_type => 'text/html'
    end


  end

  private

  def msg_router(msg)
    case msg.MsgType
      when 'text'
        text_parse(msg)
      when 'image'
        image_parse(msg)
      when 'location'
        location_parse(msg)
      when 'link'
        link_parse(msg)
      when 'event'
        event_parse(msg)
      when 'voice'
        voice_parse(msg)
      when 'video'
        video_parse(msg)
      else
    end

  end

  def check_signature


    fullpath = "#{request.protocol + request.host_with_port + request.path}"

    Rails.logger.debug "fullpath = " do
      fullpath
    end

    # 根据fullpath查询不同的token
    # token = query_token_by_fullpath(fullpath)

    Rails.logger.debug "token = " do
      token
    end

    if token.present? && Weixin::Middleware.request_is_valid?(token, params)


    else

      Rails.logger.error "token is null or request_is_valid! params = " do
        params
      end

      render :status => 401

    end

  end


end

微信接口

初始化Weixin::Client

client = Weixin::Client.new('your_weixin_app_key', 'your_weixin_app_secret')

获取access token

client.access_token

获取用户基本信息

client.user.info('openid')

发送客服消息

# 发送文本消息
message = <<STRING
{
    "touser":"OPENID",
    "msgtype":"text",
    "text":
    {
         "content":"Hello World"
    }
}
STRING

message = JSON.parse(message)

client.message_custom.send(message)


message = {"touser"=>"OPENID", "msgtype"=>"text", "text"=>{"content"=>"Hello World"}} 

client.message_custom.send(message)



# 发送图片消息
message = <<STRING
{
    "touser":"OPENID",
    "msgtype":"image",
    "image":
    {
      "media_id":"MEDIA_ID"
    }
}
STRING

message = JSON.parse(message)

client.message_custom.send(message)


message = {"touser"=>"OPENID", "msgtype"=>"image", "image"=>{"media_id"=>"MEDIA_ID"}}

client.message_custom.send(message)





# 发送语音消息
message = <<STRING
{
    "touser":"OPENID",
    "msgtype":"voice",
    "voice":
    {
      "media_id":"MEDIA_ID"
    }
}
STRING

message = JSON.parse(message)

client.message_custom.send(message)


message = {"touser"=>"OPENID", "msgtype"=>"voice", "voice"=>{"media_id"=>"MEDIA_ID"}}

client.message_custom.send(message)





# 发送视频消息
message = <<STRING
{
    "touser":"OPENID",
    "msgtype":"video",
    "video":
    {
      "media_id":"MEDIA_ID",
      "title":"TITLE",
      "description":"DESCRIPTION"
    }
}
STRING

message = JSON.parse(message)

client.message_custom.send(message)


message = {"touser"=>"OPENID", "msgtype"=>"video", "video"=>{"media_id"=>"MEDIA_ID", "title"=>"TITLE", "description"=>"DESCRIPTION"}}

client.message_custom.send(message)




# 发送音乐消息
message = <<STRING
{
    "touser":"OPENID",
    "msgtype":"music",
    "music":
    {
      "title":"MUSIC_TITLE",
      "description":"MUSIC_DESCRIPTION",
      "musicurl":"MUSIC_URL",
      "hqmusicurl":"HQ_MUSIC_URL",
      "thumb_media_id":"THUMB_MEDIA_ID" 
    }
}
STRING

message = JSON.parse(message)

client.message_custom.send(message)


message = {"touser"=>"OPENID", "msgtype"=>"music", "music"=>{"title"=>"MUSIC_TITLE", "description"=>"MUSIC_DESCRIPTION", "musicurl"=>"MUSIC_URL", "hqmusicurl"=>"HQ_MUSIC_URL", "thumb_media_id"=>"THUMB_MEDIA_ID"}}

client.message_custom.send(message)




# 发送图文消息
message = <<STRING
{
    "touser":"OPENID",
    "msgtype":"news",
    "news":{
        "articles": [
         {
             "title":"Happy Day",
             "description":"Is Really A Happy Day",
             "url":"URL",
             "picurl":"PIC_URL"
         },
         {
             "title":"Happy Day",
             "description":"Is Really A Happy Day",
             "url":"URL",
             "picurl":"PIC_URL"
         }
         ]
    }
}
STRING

message = JSON.parse(message)

client.message_custom.send(message)


message = {"touser"=>"OPENID", "msgtype"=>"news", "news"=>{"articles"=>[{"title"=>"Happy Day", "description"=>"Is Really A Happy Day", "url"=>"URL", "picurl"=>"PIC_URL"}, {"title"=>"Happy Day", "description"=>"Is Really A Happy Day", "url"=>"URL", "picurl"=>"PIC_URL"}]}} 


client.message_custom.send(message)

自定义菜单创建接口

menu_string = <<STRING
 {
     "button":[
     {  
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "type":"click",
           "name":"歌手简介",
           "key":"V1001_TODAY_SINGER"
      },
      {
           "name":"菜单",
           "sub_button":[
           {  
               "type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"
            },
            {
               "type":"view",
               "name":"视频",
               "url":"http://v.qq.com/"
            },
            {
               "type":"click",
               "name":"赞一下我们",
               "key":"V1001_GOOD"
            }]
       }]
 }
STRING

menu_hash = JSON.parse(menu_string)

menu_hash = {"button"=>[{"type"=>"click", "name"=>"今日歌曲", "key"=>"V1001_TODAY_MUSIC"}, {"type"=>"click", "name"=>"歌手简介", "key"=>"V1001_TODAY_SINGER"}, {"name"=>"菜单", "sub_button"=>[{"type"=>"view", "name"=>"搜索", "url"=>"http://www.soso.com/"}, {"type"=>"view", "name"=>"视频", "url"=>"http://v.qq.com/"}, {"type"=>"click", "name"=>"赞一下我们", "key"=>"V1001_GOOD"}]}]} 

menu.add(menu_hash)

自定义菜单查询接口

menu = Weixin::Menu.new('your_weixin_app_key', 'your_weixin_app_secret')

menu.get

自定义菜单删除接口

menu.delete

TODO

Copyright

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Bitdeli Badge

More Repositories

1

oh-my-stars

An offline CLI tool to search your GitHub Stars.
Python
94
star
2

elastic-beanstalk-nginx-uwsgi-django

A Django sample app running with uWSGI and Nginx on AWS Elastic Beanstalk.
HTML
89
star
3

my-esphome

ESPHome configurations files used in my home.
C++
15
star
4

drf-dash

Build Django REST framework docset for Dash (http://kapeli.com/dash/)
Shell
10
star
5

nodemcu-weather-station

基于 NodeMCU 实现的室内气象站,可显示室内温度、湿度、气压和室外温度、湿度、天气状况。
Lua
8
star
6

ffmpeg-wasm-build

Shell
5
star
7

Programming-Computer-Vision-with-Python-practice

Jupyter Notebook
5
star
8

drf-hashids-fields

Hashids field for Django Rest Framework Project
Python
4
star
9

AirburgEmoPlus

Domoticz plugin to read pm2.5 values from Airburg emo plus.
Python
3
star
10

rpi-airplay-display

Display the artist and name of a song on LCD (Nokia 5110)
Python
3
star
11

XiaomiAirPurifier

Domoticz plugin to control the Xiaomi Air Purifier.
Python
2
star
12

wasm-build-tool

Dockerfile
2
star
13

fmbox

Playing Douban FM on a TP-LINK WR703N Router (OpenWrt hacked)
Python
2
star
14

nodemcu-power-meter

Lua
2
star
15

my-productivity-stuff

Tools make me more productive!
2
star
16

my-fluxbox-config

my fluxbox configuration files
1
star
17

elastic-beanstalk-docker-django

A Django sample application from Docker containers running on AWS Elastic Beanstalk
1
star
18

1year-instacollage

create a collage with my instagram photos last year
Python
1
star
19

rpi-vpn-hotspot

基于树莓派搭建供安卓设备访问 Play 应用市场的私人无线热点。
Shell
1
star
20

Blackmagic-Decklink-SDK-Archive

C++
1
star
21

my-wechat-app

Python
1
star
22

GetTTS

下载有道单词发音文件
C#
1
star
23

MyBookBarcodeReader

Just scan book barcodes ...
Swift
1
star
24

pi-radio

Listen to internet music with your radio!
C
1
star
25

sunnyday-calendar

How I long for a sunny day
ApacheConf
1
star
26

spaceflight-stamp-albums

TeX
1
star
27

learn-geography

借助互联网地图服务帮助学生记忆地理知识
HTML
1
star
28

my-website

my personal website
CSS
1
star
29

rpi-homeauto-dashboard

CSS
1
star
30

mysqlclient-aws-lambda-layer

A shell script to create Lambda Layer of mysqlclient
Shell
1
star
31

wolfg1969.github.io

HTML
1
star
32

carpi

a car pc building on raspberry-pi
1
star
33

pypiserver-on-aws-elastic-beanstalk

Running a instance of pypiserver on Amazon Elastic Beanstalk
Python
1
star
34

lambda-tube-dl

📺 💾 Download Youtube Videos with AWS Lambda.
Python
1
star
35

esp32-bluetooth-speaker

Turn JBL Pebbles 1.0 into a bluetooth speaker
C++
1
star