wechat-rb

Simple Ruby wrapper for the Wechat Admin API.

Installation

$ gem install wechat-rb

Wechat API Hint

This wrapper tries to implement all available methods of the Wechat Admin API in a Ruby-like fashion. However, the Wechat API lacks a decent amount of methods that you expect an API to provide. Thus, if methods are missing or a certain implementation style was choosen it is most likely due to the inconsistency of the API itself. Feel free to get in touch or submit a pull request if you encounter any problems.

Must-known facts about the Wechat API:

  • Wechat requires multimedia files such as images and videos to be uploaded to their servers is some cases. To help make things easier, this wrapper will upload the multimedia files on the fly for you and you can even pass external url for upload.
  • Return values differ from method to method due to the way the Wechat API is implemented. Thus, a Hash as a return value or an Array of Hashes was choosen as the global return object. Basically it is a parsed JSON response.
  • Please refer to the Wechat API documentation for detailed information on parameters, return values or error codes.

Configuration and Setup

Authentication

Authenticate with the api credentials provided in your Wechat Admin Account.

Wechat.configure do |c|
  c.api_appid = 'my_appid'
  c.api_appsecret = 'my_secret'
  # OPTIONAL, defaults to https://api.weixin.qq.com/cgi-bin
  c.api_endpoint = 'https://api.weixin.qq.com/cgi-bin'
end

Interacting with the API

You can interact with the API on the provided data objects

Message

# Send a custom Text message
Wechat::Message.send({to: 'oRVIRuNafMJvn-eEwd0r9nv5HhqE'},  {text: "Hello World"})

# Send a custom Image message
Wechat::Message.send({to: 'oRVIRuNafMJvn-eEwd0r9nv5HhqE'},  {image: "http://example.com/image.jpg"})

# Send a custom News message
Wechat::Message.send({to: 'oRVIRuNafMJvn-eEwd0r9nv5HhqE'},  {news: [{title: 'My title', description: 'My descript', url: 'www.example.com', pic_url: 'http://example.com/image.jpg'}]})

Broadcast

# Send a News Message  Broadcast to all Followers
Wechat::Broadcast.send(news: [{title: 'My title', description: 'My descript', url: 'www.example.com', pic_url: 'http://example.com/image.jpg'}])

# Send a News Message  Broadcast to select Followers
Wechat::Broadcast.send(news: [{title: 'My title', description: 'My descript', url: 'www.example.com', pic_url: 'http://example.com/image.jpg'}], ['oRVIRuNafMJvn-eEwd0r9nv5HhqE'])

Follower

# Get list of Followers
Wechat::Follower.list

# Get follower profile
Wechat::Follower.show('oRVIRuNafMJvn-eEwd0r9nv5HhqE')
# Create a Custom menu
Wechat::Menu.create({button: [{type: 'click',name: 'News', key: 'news_today'}]})

Todo

  • More tests :)
  • Implement Callback Messages
  • Implement Better Error Handling and Access Token

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Ruby Versions

This gem was developed and tested with versions 2.0.0

Copyright (c) 2014 Eshaam Rabaney. See LICENSE.txt for details.