All Projects → wolfg1969 → Rack Weixin

wolfg1969 / Rack Weixin

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

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Rack Weixin

Wei
微信服务号裂变引擎,提供一套简单明了的DSL,快速配置和接入服务号裂变。 wechat fission platform, for technological dimensionality reduction.
Stars: ✭ 177 (+68.57%)
Mutual labels:  rails, weixin
Bugsnag Ruby
Bugsnag error monitoring & reporting software for rails, sinatra, rack and ruby
Stars: ✭ 211 (+100.95%)
Mutual labels:  rails, rack
Vite ruby
⚡️ Vite.js in Ruby, bringing joy to your JavaScript experience
Stars: ✭ 112 (+6.67%)
Mutual labels:  rails, rack
Split
📈 The Rack Based A/B testing framework
Stars: ✭ 2,539 (+2318.1%)
Mutual labels:  rails, rack
Rack Dev Mark
Show dev mark on development env
Stars: ✭ 350 (+233.33%)
Mutual labels:  rails, rack
Rackula
Generate a static site from any rack middleware.
Stars: ✭ 49 (-53.33%)
Mutual labels:  rails, rack
Thin
A very fast & simple Ruby web server
Stars: ✭ 2,170 (+1966.67%)
Mutual labels:  rails, rack
Rack Reducer
Declaratively filter data via URL params, in any Rack app, with any ORM.
Stars: ✭ 241 (+129.52%)
Mutual labels:  rails, rack
Lamby
Simple Rails & AWS Lambda Integration 🐑🛤
Stars: ✭ 336 (+220%)
Mutual labels:  rails, rack
Agoo
A High Performance HTTP Server for Ruby
Stars: ✭ 679 (+546.67%)
Mutual labels:  rails, rack
Letsencrypt heroku
Automated letsencrypt setup for heroku
Stars: ✭ 58 (-44.76%)
Mutual labels:  rails, rack
Activestorage Aliyun
Wraps the Aliyun OSS as an Active Storage service.
Stars: ✭ 103 (-1.9%)
Mutual labels:  rails
Pong
Two player Pong reinvented using Vue.js and Rails w/ Action Cable
Stars: ✭ 101 (-3.81%)
Mutual labels:  rails
Wechat Mp Hack
微信公众平台模拟登录自动群发图文消息
Stars: ✭ 101 (-3.81%)
Mutual labels:  weixin
Simple recommender
A simple recommendation engine for Rails/Postgres
Stars: ✭ 101 (-3.81%)
Mutual labels:  rails
Rails Letsencrypt
The Let's Encrypt certificate manager for rails
Stars: ✭ 104 (-0.95%)
Mutual labels:  rails
Coding further
"Coding further" is made for my students who graduated from Le Wagon coding bootcamp and are just now exploring the world of programming.
Stars: ✭ 103 (-1.9%)
Mutual labels:  rails
Activerecord Clean Db Structure
Automatic cleanup for the Rails db/structure.sql file (ActiveRecord/PostgreSQL)
Stars: ✭ 101 (-3.81%)
Mutual labels:  rails
Graphql devise
GraphQL interface on top devise_token_auth
Stars: ✭ 100 (-4.76%)
Mutual labels:  rails
Material design lite Sass
Google's Material Design Lite with Material Icons and Roboto font for Ruby applications
Stars: ✭ 100 (-4.76%)
Mutual labels:  rails

微信公众平台 开放消息接口 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].