Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dingtalk-sdk
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
dingtalk-sdk
Commits
59713a96
Commit
59713a96
authored
Oct 29, 2020
by
Francis Zhou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add group robot message builder class
parent
50cda580
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
103 additions
and
3 deletions
+103
-3
robot.rb
lib/dingtalk_sdk/robot.rb
+103
-3
No files found.
lib/dingtalk_sdk/robot.rb
View file @
59713a96
# frozen_string_literal: true
require
'json'
require
'base64'
require
'openssl'
require
'dingtalk_sdk'
require
'dingtalk_sdk/core'
require
'dingtalk_sdk/request_url'
require
'active_support/core_ext/object'
require
'active_support/core_ext/integer'
require
'active_support/core_ext/time/zones'
module
DingtalkSdk
module
Robot
extend
DingtalkSdk
::
Core
def
self
.
calculate_signature
(
secret
,
timestamp
=
nil
)
timestamp
=
Time
.
zone
.
now
.
to_i
if
timestamp
.
nil?
def
self
.
calculate_signature
(
secret
,
timestamp
)
raise
ArgumentError
,
'timestamp must in millis'
if
Math
.
log10
(
timestamp
).
ceil
<
13
origin_str
=
[
timestamp
*
1000
,
secret
].
join
(
"
\n
"
)
origin_str
=
[
timestamp
,
secret
].
join
(
"
\n
"
)
signature_str
=
OpenSSL
::
HMAC
.
digest
(
'SHA256'
,
secret
,
origin_str
)
signature_str_base64
=
Base64
.
strict_encode64
(
signature_str
)
Signature
.
new
(
signature_str_base64
)
end
def
self
.
verify_signature
(
secret
,
signature
,
timestamp
=
Time
.
zone
.
now
)
return
false
if
Time
.
zone
.
now
-
timestamp
>
1
.
hour
calculate_signature
(
secret
,
timestamp
.
to_i
*
1000
)
==
signature
end
class
MessageBuilder
def
initialize
@mesg
=
nil
@is_at_all
=
false
@at_mobile_list
=
nil
end
def
text
(
text
:)
@mesg
=
{
msgtype:
'text'
,
text:
{
content:
text
}
}
end
def
markdown
(
title
:,
text
:)
@mesg
=
{
msgtype:
'markdown'
,
markdown:
{
title:
title
,
text:
text
}
}
end
def
action_card_entire_link
(
title
:,
text
:,
single_title
:,
single_url
:,
btn_orientation:
'0'
)
raise
ArgumentError
,
'btn_orientation only accept 0 or 1'
unless
%w[0 1]
.
include?
(
btn_orientation
)
@mesg
=
{
msgtype:
'actionCard'
,
actionCard:
{
title:
title
,
text:
text
,
singleTitle:
single_title
,
singleURL:
single_url
,
btnOrientation:
btn_orientation
}
}
end
def
action_card_separate_link
(
title
:,
text
:,
btn_orientation:
'0'
,
btns
:)
raise
ArgumentError
,
'btn_orientation only accept 0 or 1'
unless
%w[0 1]
.
include?
(
btn_orientation
)
raise
ArgumentError
,
'btns must be an array of hash with title and actionURL'
\
unless
btns
.
all?
{
|
i
|
(
%i[title actionURL]
&
[
*
i
.
try
(
:keys
)]).
any?
}
@mesg
=
{
msgtype:
'actionCard'
,
actionCard:
{
title:
title
,
text:
text
,
btnOrientation:
btn_orientation
,
btns:
[
*
btns
]
}
}
end
def
feed_card
(
links
:)
@mesg
=
{
msgtype:
'feedCard'
,
feedCard:
{
links:
links
}
}
end
def
at_mobile
(
mobile
)
@is_at_all
=
false
@at_mobile_list
=
[
*
mobile
]
end
def
at_all
@is_at_all
=
true
@at_mobile_list
=
nil
end
def
to_h
{}.
tap
do
|
h
|
h
.
merge!
@mesg
if
@is_at_all
h
[
:isAtAll
]
=
true
elsif
@at_mobile_list
.
try
(
:size
).
positive?
h
[
:atMobiles
]
=
@at_mobile_list
end
end
end
def
to_json
(
*
_args
)
JSON
.
generate
to_h
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment