class WechatWorkWebhook

Constants

VERSION

Public Class Methods

new(webhook_url) click to toggle source
# File lib/wechat_work_webhook.rb, line 10
def initialize(webhook_url)
  @webhook_url = webhook_url
end

Public Instance Methods

file(file_path) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = []) click to toggle source
# 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) click to toggle source
# 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