Class: WechatWorkWebhook

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

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(webhook_url) ⇒ WechatWorkWebhook

Returns a new instance of WechatWorkWebhook.



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

def initialize(webhook_url)
  @webhook_url = webhook_url
end

Instance Method Details

#file(file_path) ⇒ Object



75
76
77
78
# File 'lib/wechat_work_webhook.rb', line 75

def file(file_path)
  media_id = upload_media(file_path)['media_id']
  media(media_id)
end

#image(image_path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wechat_work_webhook.rb', line 46

def image(image_path)
  image_base64 = Base64.strict_encode64(File.read(image_path))
  image_md5 = Digest::MD5.hexdigest(File.read(image_path))

  data = {
      "msgtype": "image",
      "image": {
          "base64": image_base64,
          "md5": image_md5
      }
  }
  HTTParty.post(@webhook_url, body: data.to_json).parsed_response
end

#markdown(markdown) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/wechat_work_webhook.rb', line 26

def markdown(markdown)
  data = {
      "msgtype" => "markdown",
      "markdown" => {
          "content" => markdown
      }
  }
  HTTParty.post(@webhook_url, body: data.to_json).parsed_response
end

#media(media_id) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/wechat_work_webhook.rb', line 60

def media(media_id)
  data = {
      "msgtype" => "file",
      "file" => {
          "media_id" => media_id
      }
  }
  HTTParty.post(@webhook_url, body: data.to_json).parsed_response
end

#news(articles) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/wechat_work_webhook.rb', line 36

def news(articles)
  data = {
      "msgtype" => "news",
      "news" => {
          "articles" => articles
      }
  }
  HTTParty.post(@webhook_url, body: data.to_json).parsed_response
end

#text(text, mentioned_list = [], mentioned_mobile_list = []) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wechat_work_webhook.rb', line 14

def text(text, mentioned_list = [], mentioned_mobile_list = [])
  data = {
      "msgtype" => "text",
      "text" => {
          "content" => text,
          "mentioned_list" => mentioned_list,
          "mentioned_mobile_list" => mentioned_mobile_list
      }
  }
  HTTParty.post(@webhook_url, body: data.to_json).parsed_response
end

#upload_media(file_path) ⇒ Object



70
71
72
73
# File 'lib/wechat_work_webhook.rb', line 70

def upload_media(file_path)
  upload_url = @webhook_url.sub('send', 'upload_media') + '&type=file'
  HTTParty.post(upload_url, body: { media: File.open(file_path)}).parsed_response
end