Sha256: 194b574dbd34b2bd15f2be7e198efada334e9527460d20773d584324f3294d4a

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module OceanWechatRobot

  class WechatRobot
    require 'httparty'

    attr_accessor :webhook_url

    # 初始化
    def initialize(webhook_url)
      @webhook_url = webhook_url
    end

    # 处理手机号
    def handle_at_mobiles(at_mobiles = [], is_at_all = false)
      mobiles = []
      if is_at_all
        mobiles.push("@all")
      else
        mobiles.concat(at_mobiles)
      end
      mobiles
    end

    def send_text(content = '', at_mobiles = [], is_at_all = false)
      mobile_list = handle_at_mobiles(at_mobiles, is_at_all)
      data = {
          "msgtype" => "text",
          "text" => {
              "content" => content,
              "mentioned_mobile_list" => mobile_list
          }
      }
      HTTParty.post(@webhook_url, body: data.to_json).parsed_response
    end

    def send_markdown(content = '', at_mobiles = [])
      data = {
          "msgtype" => "markdown",
          "markdown" => {
              "content" => content
          }
      }
      HTTParty.post(@webhook_url, body: data.to_json).parsed_response

      # 再发送@人的消息
      if !at_mobiles.empty?
        send_text('', at_mobiles)
      end
    end

    def send_news(title = '', description = '', url = '', picurl = '', at_mobiles = [])
      data = {
          "msgtype" => "news",
          "news" => {
              "articles" => [
                  {
                      "title" => title,
                      "description" => description,
                      "url" => url,
                      "picurl" => picurl
                  }
              ]
          }
      }

      HTTParty.post(@webhook_url, body: data.to_json).parsed_response

      # 再发送@人的消息
      if !at_mobiles.empty?
        send_text('', at_mobiles)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ocean_wechat_robot-0.0.1 lib/ocean_wechat_robot/wechat.rb