Class: OceanWechatRobot::WechatRobot

Inherits:
Object
  • Object
show all
Defined in:
lib/ocean_wechat_robot/wechat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webhook_url) ⇒ WechatRobot

初始化



10
11
12
# File 'lib/ocean_wechat_robot/wechat.rb', line 10

def initialize(webhook_url)
  @webhook_url = webhook_url
end

Instance Attribute Details

#webhook_urlObject

Returns the value of attribute webhook_url.



7
8
9
# File 'lib/ocean_wechat_robot/wechat.rb', line 7

def webhook_url
  @webhook_url
end

Instance Method Details

#handle_at_mobiles(at_mobiles = [], is_at_all = false) ⇒ Object

处理手机号



15
16
17
18
19
20
21
22
23
# 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 = []) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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 = []) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# 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