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 +4 -4
- data/.gitignore +1 -0
- data/README.md +72 -39
- data/dingbot.gemspec +2 -1
- data/lib/dingbot.rb +5 -5
- data/lib/dingbot/client.rb +24 -3
- data/lib/dingbot/configuration.rb +40 -0
- data/lib/dingbot/error.rb +1 -1
- data/lib/dingbot/message/action_card.rb +16 -12
- data/lib/dingbot/message/link.rb +8 -6
- data/lib/dingbot/message/markdown.rb +6 -4
- data/lib/dingbot/message/text.rb +12 -10
- data/lib/dingbot/version.rb +1 -1
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f9a46ca05f2961d757a47927595c3300ebd59b1
|
4
|
+
data.tar.gz: 7a5114129dcfe3883f9269419f233a7d701a2609
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9de7eead38333a685c6b28f5e53d0edf81193201ea7434647045d7b166a50bdf0eeb652964f54699b39bfe761a1873e7b4e3ae43caa07c664183d7b281f79d45
|
7
|
+
data.tar.gz: 35f8cbd6429e2197e3e44463188c323d29e76fc13904ee41093c520cad2cc07a689d09e0c20bbb8e36b4d6bec35174a7baa324f3aaaf061b304a30495bc60ec8
|
data/.gitignore
CHANGED
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
|
-
|
26
|
-
|
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
|
-
#
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
46
|
-
|
62
|
+
def send_markdown
|
63
|
+
DingBot.send_markdown('我就是我, 是不一样的烟火', '### 我就是我, 是不一样的烟火')
|
64
|
+
end
|
47
65
|
|
48
66
|
# 发送整体跳转ActionCard消息
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
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(
|
18
|
-
DingBot::Client.new(
|
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|
|
41
|
+
hidden = /access_token|post|validate|httparty/
|
42
42
|
(DingBot::Client.instance_methods - Object.methods).reject {|e| e[hidden]}
|
43
43
|
end
|
44
44
|
|
data/lib/dingbot/client.rb
CHANGED
@@ -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
|
-
|
15
|
-
|
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(
|
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
@@ -17,12 +17,14 @@ module DingBot
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def body_params
|
20
|
-
super.merge(
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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='',
|
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
|
-
|
42
|
-
|
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'
|
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
|
data/lib/dingbot/message/link.rb
CHANGED
@@ -26,12 +26,14 @@ module DingBot
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def body_params
|
29
|
-
super.merge(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
|
data/lib/dingbot/message/text.rb
CHANGED
@@ -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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
|
data/lib/dingbot/version.rb
CHANGED
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.
|
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-
|
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
|
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
|
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:
|
129
|
+
version: 2.0.0
|
129
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
131
|
requirements:
|
131
132
|
- - ">="
|