dingbot 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6fde2528dbfb40522dd9de78bf0445be9a091ec1
4
- data.tar.gz: da075b21786be72f62909ffeefa5195e33c09c02
3
+ metadata.gz: 7f9a46ca05f2961d757a47927595c3300ebd59b1
4
+ data.tar.gz: 7a5114129dcfe3883f9269419f233a7d701a2609
5
5
  SHA512:
6
- metadata.gz: 2cff4f93367b5cc9f44a092e1059bbd8294df0a4c8ecf1cd0f249090a52e53b16e9a388e219df23632e23879474dc19b39de5ef007ae8961236e0a2bf23a0bce
7
- data.tar.gz: ea07b506bcee57bde8a0d3b886a0aa15469f36ba1ff800d7658c35e8cfa60fcd17e1ca64c123336e15b0cc810582316f4278730a7f15769f9a5738c8fa3ee355
6
+ metadata.gz: 9de7eead38333a685c6b28f5e53d0edf81193201ea7434647045d7b166a50bdf0eeb652964f54699b39bfe761a1873e7b4e3ae43caa07c664183d7b281f79d45
7
+ data.tar.gz: 35f8cbd6429e2197e3e44463188c323d29e76fc13904ee41093c520cad2cc07a689d09e0c20bbb8e36b4d6bec35174a7baa324f3aaaf061b304a30495bc60ec8
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  .idea
11
+ *.gem
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # DingTalk Bot
2
+ [![Gem Version](https://badge.fury.io/rb/dingbot.svg)](https://badge.fury.io/rb/dingbot.svg)
3
+ [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/thierryxing/dingtalk-bot/master/LICENSE.txt)
2
4
 
3
5
  DingTalk Bot是阿里钉钉自定义机器人的Ruby库
4
6
  官方文档:[阿里钉钉自定义机器人](https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.karFPe&treeId=257&articleId=105735&docType=1).
@@ -22,56 +24,87 @@ gem 'dingbot'
22
24
  初始化客户端:
23
25
 
24
26
  ```ruby
25
- access_token = 'xxxxxxxxxxxxxxx'
26
- client = DingBot.client(access_token)
27
+ DingBot.configure do |config|
28
+ config.endpoint = 'https://oapi.dingtalk.com/robot/send' # API endpoint URL, default: ENV['DINGTALK_API_ENDPOINT'] or https://oapi.dingtalk.com/robot/send
29
+ config.access_token = '3ddef428f1478056e858450e07272834c79bd538e8055a04e989573c469xxxx' # access token, default: ENV['DINGTALK_ACCESS_TOKEN']
30
+ end
27
31
  ```
28
32
 
29
33
  发送消息
30
34
  ```ruby
31
- # 发送Text消息
32
- message = DingBot::Message::Text.new('我就是我, 是不一样的烟火',
33
- ['156xxxx8827'],
34
- false)
35
- client.send_msg(message)
35
+ # 快速发送一套只包含文本的Text消息(不需要构造Message对象)
36
+ def send_simple_text
37
+ DingBot.send_text('我就是我, 是不一样的烟火')
38
+ end
39
+
40
+ # 发送复杂Text消息
41
+ def send_text
42
+ message = DingBot::Message::Text.new(
43
+ '我就是我, 是不一样的烟火',
44
+ ['13718896117'],
45
+ false
46
+ )
47
+ DingBot.send_msg(message)
48
+ end
36
49
 
37
50
  # 发送Link消息
38
- message = DingBot::Message::Link.new('我就是我, 是不一样的烟火',
39
- '这个即将发布的新版本,创始人陈航(花名“无招”)称它为“红树林”。',
40
- 'https://www.dingtalk.com/',
41
- 'https://avatars1.githubusercontent.com/u/64818')
42
- client.send_msg(message)
51
+ def send_link
52
+ message = DingBot::Message::Link.new(
53
+ '我就是我, 是不一样的烟火',
54
+ '这个即将发布的新版本,创始人陈航(花名“无招”)称它为“红树林”。',
55
+ 'https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI',
56
+ 'https://avatars1.githubusercontent.com/u/64818'
57
+ )
58
+ DingBot.send_msg(message)
59
+ end
43
60
 
44
61
  # 发送Markdown消息
45
- message = DingBot::Message::Markdown.new('我就是我, 是不一样的烟火', '### 我就是我, 是不一样的烟火')
46
- client.send_msg(message)
62
+ def send_markdown
63
+ DingBot.send_markdown('我就是我, 是不一样的烟火', '### 我就是我, 是不一样的烟火')
64
+ end
47
65
 
48
66
  # 发送整体跳转ActionCard消息
49
- message = DingBot::Message::WholeActionCard.new('乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身',
50
- '![screenshot](@lADOpwk3K80C0M0FoA) ### 乔布斯 20 年前想打造的苹果咖啡厅',
51
- '0',
52
- '0',
53
- '阅读全文',
54
- 'https://www.dingtalk.com/')
55
- client.send_msg(message)
56
-
67
+ def send_whole_action_card
68
+ message = DingBot::Message::WholeActionCard.new(
69
+ '乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身',
70
+ '![screenshot](@lADOpwk3K80C0M0FoA) ### 乔布斯 20 年前想打造的苹果咖啡厅',
71
+ '阅读全文',
72
+ 'https://www.dingtalk.com/'
73
+ )
74
+ DingBot.send_msg(message)
75
+ end
76
+
57
77
  # 发送独立跳转ActionCard类型消息
58
- message = DingBot::Message::IndependentActionCard.new('乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身',
59
- '![screenshot](@lADOpwk3K80C0M0FoA) ### 乔布斯 20 年前想打造的苹果咖啡厅',
60
- '0',
61
- '0',
62
- [
63
- DingBot::Message::ActionBtn.new('内容不错', 'https://www.dingtalk.com/'),
64
- DingBot::Message::ActionBtn.new('不感兴趣', 'https://www.dingtalk.com/')
65
- ])
66
- client.send_msg(message)
67
-
68
- # 发送FeedCard类型
69
- message = DingBot::Message::FeedCard.new([
70
- DingBot::Message::FeedCardLink.new('时代的火车向前开',
71
- 'https://avatars1.githubusercontent.com/u/64818',
72
- 'https://www.dingtalk.com/')
73
- ])
74
- client.send_msg(message)
78
+ def send_independent_action_card
79
+ message = DingBot::Message::IndependentActionCard.new(
80
+ '乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身',
81
+ '![screenshot](@lADOpwk3K80C0M0FoA) ### 乔布斯 20 年前想打造的苹果咖啡厅',
82
+ [
83
+ DingBot::Message::ActionBtn.new('内容不错', 'https://www.dingtalk.com/'),
84
+ DingBot::Message::ActionBtn.new('不感兴趣', 'https://www.dingtalk.com/')
85
+ ]
86
+ )
87
+ DingBot.send_msg(message)
88
+ end
89
+
90
+ # 发送FeedCard类型
91
+ def send_feed_card
92
+ message = DingBot::Message::FeedCard.new(
93
+ [
94
+ DingBot::Message::FeedCardLink.new(
95
+ '时代的火车向前开',
96
+ 'https://avatars1.githubusercontent.com/u/64818',
97
+ 'https://www.dingtalk.com/'
98
+ ),
99
+ DingBot::Message::FeedCardLink.new(
100
+ '时代的火车向前开',
101
+ 'https://avatars1.githubusercontent.com/u/64818',
102
+ 'https://www.dingtalk.com/'
103
+ )
104
+ ]
105
+ )
106
+ DingBot.send_msg(message)
107
+ end
75
108
  ```
76
109
  更为详细的用法请见此链接:[https://github.com/thierryxing/dingtalk-bot/blob/master/test/dingbot_test.rb](https://github.com/thierryxing/dingtalk-bot/blob/master/test/dingbot_test.rb)
77
110
 
data/dingbot.gemspec CHANGED
@@ -29,8 +29,9 @@ Gem::Specification.new do |spec|
29
29
  spec.bindir = "exe"
30
30
  spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f)}
31
31
  spec.require_paths = ["lib"]
32
+ spec.required_ruby_version = ">= 2.0.0"
32
33
 
33
- spec.add_runtime_dependency 'httparty', "~> 0.15.5"
34
+ spec.add_runtime_dependency "httparty"
34
35
  spec.add_development_dependency "bundler", "~> 1.14"
35
36
  spec.add_development_dependency "rake", "~> 10.0"
36
37
  spec.add_development_dependency "minitest", "~> 5.0"
data/lib/dingbot.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "dingbot/version"
2
2
  require 'dingbot/error'
3
3
  require 'dingbot/client'
4
+ require 'dingbot/configuration'
4
5
  require 'dingbot/message/text'
5
6
  require 'dingbot/message/link'
6
7
  require 'dingbot/message/markdown'
@@ -8,14 +9,13 @@ require 'dingbot/message/action_card'
8
9
  require 'dingbot/message/feed_card'
9
10
 
10
11
  module DingBot
11
-
12
- ENDPOINT = "https://oapi.dingtalk.com/robot/send"
12
+ extend Configuration
13
13
 
14
14
  # Alias for DingBot::Client.new
15
15
  #
16
16
  # @return [DingBot::Client]
17
- def self.client(access_token='')
18
- DingBot::Client.new(access_token)
17
+ def self.client(options={})
18
+ DingBot::Client.new(options)
19
19
  end
20
20
 
21
21
  # Delegate to DingBot::Client
@@ -38,7 +38,7 @@ module DingBot
38
38
  #
39
39
  # @return [Array<Symbol>]
40
40
  def self.actions
41
- hidden = /access_token|post|validate|set_request_defaults|httparty/
41
+ hidden = /access_token|post|validate|httparty/
42
42
  (DingBot::Client.instance_methods - Object.methods).reject {|e| e[hidden]}
43
43
  end
44
44
 
@@ -1,5 +1,8 @@
1
1
  require 'httparty'
2
+ require 'dingbot/configuration'
2
3
  require 'dingbot/message/base'
4
+ require 'dingbot/message/text'
5
+ require 'dingbot/message/markdown'
3
6
 
4
7
  module DingBot
5
8
  # @private
@@ -11,8 +14,16 @@ module DingBot
11
14
 
12
15
  attr_accessor :access_token
13
16
 
14
- def initialize(access_token='')
15
- @access_token = access_token
17
+ # @private
18
+ attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
19
+
20
+ # Creates a new API.
21
+ # @raise [Error:MissingCredentials]
22
+ def initialize(options={})
23
+ options = DingBot.options.merge(options)
24
+ (Configuration::VALID_OPTIONS_KEYS).each do |key|
25
+ send("#{key}=", options[key]) if options[key]
26
+ end
16
27
  end
17
28
 
18
29
  # Parse response body.
@@ -32,7 +43,17 @@ module DingBot
32
43
  end
33
44
 
34
45
  def send_msg(message)
35
- validate self.class.post(DingBot::ENDPOINT, {query: {access_token: @access_token}, body: message.to_json})
46
+ validate self.class.post(@endpoint, {query: {access_token: @access_token}, body: message.to_json})
47
+ end
48
+
49
+ def send_text(content)
50
+ message = DingBot::Message::Text.new(content)
51
+ send_msg(message)
52
+ end
53
+
54
+ def send_markdown(title, text)
55
+ message = DingBot::Message::Markdown.new(title, text)
56
+ send_msg(message)
36
57
  end
37
58
 
38
59
  # Checks the response code for common errors.
@@ -0,0 +1,40 @@
1
+ module DingBot
2
+ # Defines constants and methods related to configuration.
3
+ module Configuration
4
+ # An array of valid keys in the options hash when configuring a Sentry::API.
5
+ VALID_OPTIONS_KEYS = [:endpoint, :access_token, :httparty].freeze
6
+
7
+ # The user agent that will be sent to the API endpoint if none is set.
8
+ DEFAULT_USER_AGENT = "DingBot Ruby Gem #{DingBot::VERSION}".freeze
9
+
10
+ DEFAULT_ENDPOINT = "https://oapi.dingtalk.com/robot/send"
11
+
12
+ # @private
13
+ attr_accessor(*VALID_OPTIONS_KEYS)
14
+
15
+ # Sets all configuration options to their default values
16
+ # when this module is extended.
17
+ def self.extended(base)
18
+ base.reset
19
+ end
20
+
21
+ # Convenience method to allow configuration options to be set in a block.
22
+ def configure
23
+ yield self
24
+ end
25
+
26
+ # Creates a hash of options and their values.
27
+ def options
28
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
29
+ option.merge!(key => send(key))
30
+ end
31
+ end
32
+
33
+ # Resets all configuration options to the defaults.
34
+ def reset
35
+ self.endpoint = ENV['DINGTALK_ENDPOINT'] || DEFAULT_ENDPOINT
36
+ self.access_token = ENV['DINGTALK_ACCESS_TOKEN']
37
+ end
38
+
39
+ end
40
+ end
data/lib/dingbot/error.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module DingBot
2
2
  module Error
3
- # Custom error class for rescuing from all Sentry errors.
3
+ # Custom error class for rescuing from all errors.
4
4
  class Error < StandardError;
5
5
  end
6
6
 
@@ -17,12 +17,14 @@ module DingBot
17
17
  end
18
18
 
19
19
  def body_params
20
- super.merge(actionCard: {
21
- title: @title,
22
- text: @text,
23
- "hideAvatar": @hide_avatar,
24
- "btnOrientation": @btn_orientation,
25
- })
20
+ super.merge(
21
+ actionCard: {
22
+ title: @title,
23
+ text: @text,
24
+ "hideAvatar": @hide_avatar,
25
+ "btnOrientation": @btn_orientation,
26
+ }
27
+ )
26
28
  end
27
29
  end
28
30
 
@@ -30,17 +32,19 @@ module DingBot
30
32
  class WholeActionCard < ActionCard
31
33
  attr_accessor :single_title, :single_url
32
34
 
33
- def initialize(title='', text='', btn_orientation='0', hide_avatar='0', single_title='', single_url='')
35
+ def initialize(title='', text='', single_title='', single_url='', btn_orientation='0', hide_avatar='0')
34
36
  super(title, text, btn_orientation, hide_avatar)
35
37
  @single_title = single_title
36
38
  @single_url = single_url
37
39
  end
38
40
 
39
41
  def body_params
40
- action_card = super[:actionCard].merge({
41
- singleTitle: @single_title,
42
- singleURL: @single_url
43
- })
42
+ action_card = super[:actionCard].merge(
43
+ {
44
+ singleTitle: @single_title,
45
+ singleURL: @single_url
46
+ }
47
+ )
44
48
  super.merge(actionCard: action_card)
45
49
  end
46
50
  end
@@ -49,7 +53,7 @@ module DingBot
49
53
  class IndependentActionCard < ActionCard
50
54
  attr_accessor :buttons
51
55
 
52
- def initialize(title='', text='', btn_orientation='0', hide_avatar='0', buttons=[])
56
+ def initialize(title='', text='', buttons=[], btn_orientation='0', hide_avatar='0')
53
57
  super(title, text, btn_orientation, hide_avatar)
54
58
  @buttons = buttons
55
59
  end
@@ -26,12 +26,14 @@ module DingBot
26
26
  end
27
27
 
28
28
  def body_params
29
- super.merge(link: {
30
- text: @text,
31
- title: @title,
32
- picUrl: @pic_url,
33
- messageUrl: @message_url
34
- })
29
+ super.merge(
30
+ link: {
31
+ text: @text,
32
+ title: @title,
33
+ picUrl: @pic_url,
34
+ messageUrl: @message_url
35
+ }
36
+ )
35
37
  end
36
38
  end
37
39
 
@@ -25,10 +25,12 @@ module DingBot
25
25
  end
26
26
 
27
27
  def body_params
28
- super.merge(markdown: {
29
- text: @text,
30
- title: @title,
31
- })
28
+ super.merge(
29
+ markdown: {
30
+ text: @text,
31
+ title: @title,
32
+ }
33
+ )
32
34
  end
33
35
  end
34
36
 
@@ -17,7 +17,7 @@ module DingBot
17
17
  # }
18
18
  class Text < Base
19
19
  attr_accessor :content, :at_mobiles, :is_at_all
20
-
20
+
21
21
  def initialize(content='', at_mobiles=[], is_at_all=false)
22
22
  @content = content
23
23
  @at_mobiles = at_mobiles
@@ -29,15 +29,17 @@ module DingBot
29
29
  end
30
30
 
31
31
  def body_params
32
- super.merge({
33
- text: {
34
- "content": @content
35
- },
36
- at: {
37
- atMobiles: @at_mobiles,
38
- isAtAll: @is_at_all
39
- }
40
- })
32
+ super.merge(
33
+ {
34
+ text: {
35
+ "content": @content
36
+ },
37
+ at: {
38
+ atMobiles: @at_mobiles,
39
+ isAtAll: @is_at_all
40
+ }
41
+ }
42
+ )
41
43
  end
42
44
  end
43
45
 
@@ -1,5 +1,5 @@
1
1
  module DingBot
2
2
 
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
 
5
5
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dingbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thierry Xing
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-18 00:00:00.000000000 Z
11
+ date: 2017-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.15.5
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.15.5
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -104,6 +104,7 @@ files:
104
104
  - dingbot.gemspec
105
105
  - lib/dingbot.rb
106
106
  - lib/dingbot/client.rb
107
+ - lib/dingbot/configuration.rb
107
108
  - lib/dingbot/error.rb
108
109
  - lib/dingbot/message/action_card.rb
109
110
  - lib/dingbot/message/base.rb
@@ -125,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
126
  requirements:
126
127
  - - ">="
127
128
  - !ruby/object:Gem::Version
128
- version: '0'
129
+ version: 2.0.0
129
130
  required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  requirements:
131
132
  - - ">="