class OceanWechatRobot::WechatRobot

Attributes

webhook_url[RW]

Public Class Methods

new(webhook_url) click to toggle source

初始化

# File lib/ocean_wechat_robot/wechat.rb, line 10
def initialize(webhook_url)
  @webhook_url = webhook_url
end

Public Instance Methods

handle_at_mobiles(at_mobiles = [], is_at_all = false) click to toggle source

处理手机号

# File lib/ocean_wechat_robot/wechat.rb, line 15
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
send_markdown(content = '', at_mobiles = []) click to toggle source
# File lib/ocean_wechat_robot/wechat.rb, line 37
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
send_news(title = '', description = '', url = '', picurl = '', at_mobiles = []) click to toggle source
# File lib/ocean_wechat_robot/wechat.rb, line 52
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
send_text(content = '', at_mobiles = [], is_at_all = false) click to toggle source
# File lib/ocean_wechat_robot/wechat.rb, line 25
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