Skip to content

Commit

Permalink
feat: add bot message with sign
Browse files Browse the repository at this point in the history
  • Loading branch information
gazedreamily committed May 8, 2023
1 parent bc0ddc1 commit 343599a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
2 changes: 2 additions & 0 deletions lib/feishu-api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
require 'feishu-api/api'

require 'httparty'
require 'openssl'
require 'base64'

module FeishuApi
# Your code goes here...
Expand Down
61 changes: 34 additions & 27 deletions lib/feishu-api/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,78 +22,78 @@ def post(url, data, headers = {}, timeout = 30)
HTTParty.post(api(url),
body: data.to_json,
headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }.merge(headers),
timeout: timeout,
timeout: ,
verify: false)
end

def post_with_token(url, data, headers = {}, timeout = 30)
HTTParty.post(api(url),
body: data,
headers: { Authorization: "Bearer #{tenant_access_token}" }.merge(headers),
timeout: timeout)
timeout: )
end

def post_with_user_token(url, user_access_token, data, headers = {}, timeout = 30)
HTTParty.post(api(url),
body: data,
headers: { Authorization: "Bearer #{user_access_token}" }.merge(headers),
timeout: timeout)
timeout: )
end

def get_with_token(url, data = {}, headers = {}, timeout = 30)
HTTParty.get(api(url),
body: data,
headers: { Authorization: "Bearer #{tenant_access_token}" }.merge(headers),
timeout: timeout)
timeout: )
end

def get_with_user_token(url, user_access_token, data = {}, headers = {}, timeout = 30)
HTTParty.get(api(url),
body: data,
headers: { Authorization: "Bearer #{user_access_token}" }.merge(headers),
timeout: timeout)
timeout: )
end

def put_with_token(url, data = {}, headers = {}, timeout = 30)
HTTParty.put(api(url),
body: data,
headers: { Authorization: "Bearer #{tenant_access_token}" }.merge(headers),
timeout: timeout)
timeout: )
end

def put_with_user_token(url, user_access_token, data = {}, headers = {}, timeout = 30)
HTTParty.put(api(url),
body: data,
headers: { Authorization: "Bearer #{user_access_token}" }.merge(headers),
timeout: timeout)
timeout: )
end

def delete_with_token(url, data = {}, headers = {}, timeout = 30)
HTTParty.delete(api(url),
body: data,
headers: { Authorization: "Bearer #{tenant_access_token}" }.merge(headers),
timeout: timeout)
timeout: )
end

def delete_with_user_token(url, user_access_token, data = {}, headers = {}, timeout = 30)
HTTParty.delete(api(url),
body: data,
headers: { Authorization: "Bearer #{user_access_token}" }.merge(headers),
timeout: timeout)
timeout: )
end

def patch_with_token(url, data = {}, headers = {}, timeout = 30)
HTTParty.patch(api(url),
body: data,
headers: { Authorization: "Bearer #{tenant_access_token}" }.merge(headers),
timeout: timeout)
timeout: )
end

def patch_with_user_token(url, user_access_token, data = {}, headers = {}, timeout = 30)
HTTParty.patch(api(url),
body: data,
headers: { Authorization: "Bearer #{user_access_token}" }.merge(headers),
timeout: timeout)
timeout: )
end

def tenant_access_token
Expand Down Expand Up @@ -128,7 +128,7 @@ def app_access_token
# 发送消息
def send_message(receive_type, receive_id, msg_type, content)
res = post_with_token("#{API_SEND_MESSAGES}?receive_id_type=#{receive_type}",
{ receive_id: receive_id, msg_type: msg_type, content: content })
{ receive_id: , msg_type: , content: })
# p res
return nil if res.code != 200

Expand All @@ -142,7 +142,7 @@ def send_message_by_group(receive_id, msg_type, content)

# 发文本消息到指定群聊
def send_text_by_group(receive_id, text)
send_message_by_group(receive_id, 'text', JSON.generate({ text: text }))
send_message_by_group(receive_id, 'text', JSON.generate({ text: }))
end

# 上传图片
Expand Down Expand Up @@ -171,7 +171,7 @@ def download_file(file_key)

# 发文件消息到指定群聊
def send_file_by_group(receive_id, file_key)
send_message_by_group(receive_id, 'file', JSON.generate({ file_key: file_key }))
send_message_by_group(receive_id, 'file', JSON.generate({ file_key: }))
end

# 撤回消息
Expand All @@ -186,7 +186,7 @@ def check_reader(message_id)

# 发图片消息到指定群聊
def send_image_by_group(receive_id, image_key)
send_message_by_group(receive_id, 'image', JSON.generate({ image_key: image_key }))
send_message_by_group(receive_id, 'image', JSON.generate({ image_key: }))
end

# 回复消息
Expand Down Expand Up @@ -294,13 +294,13 @@ def dissolve_group(chat_id)
# 将用户或机器人拉入群聊
def add_group_member(chat_id, id_list)
post_with_token("#{API_CHATS}/#{chat_id}/members",
{ id_list: id_list }.to_json, { 'Content-Type' => 'application/json', 'Accept' => 'application/json' })
{ id_list: }.to_json, { 'Content-Type' => 'application/json', 'Accept' => 'application/json' })
end

# 将用户或机器人移出群聊
def delete_group_member(chat_id, id_list)
delete_with_token("#{API_CHATS}/#{chat_id}/members",
{ id_list: id_list }.to_json, { 'Content-Type' => 'application/json', 'Accept' => 'application/json' })
{ id_list: }.to_json, { 'Content-Type' => 'application/json', 'Accept' => 'application/json' })
end

# 用户或机器人主动加入群聊
Expand All @@ -317,13 +317,13 @@ def member_is_in_chat(chat_id)
# 指定群管理员
def add_group_managers(chat_id, manager_ids)
post_with_token("#{API_CHATS}/#{chat_id}/managers/add_managers",
{ manager_ids: manager_ids }.to_json, { 'Content-Type' => 'application/json' })
{ manager_ids: }.to_json, { 'Content-Type' => 'application/json' })
end

# 删除群管理员
def delete_group_managers(chat_id, manager_ids)
post_with_token("#{API_CHATS}/#{chat_id}/managers/delete_managers",
{ manager_ids: manager_ids }.to_json, { 'Content-Type' => 'application/json' })
{ manager_ids: }.to_json, { 'Content-Type' => 'application/json' })
end

# 获取群成员列表
Expand Down Expand Up @@ -355,8 +355,15 @@ def send_message_by_email(receive_id, msg_type, content)
end

# 自定义机器人接口 发送消息
def custom_robot_send(data, hook_id)
post("#{API_CUSTOM_BOT_SEND}/#{hook_id}", data)
def custom_robot_send(data, hook_id, secret = '')
if secret != ''
timestamp = Time.now.to_i.to_s
string_to_sign = "#{timestamp}\n#{secret}"
hmac_code = OpenSSL::HMAC.digest('sha256', string_to_sign, "")
sign = Base64.encode64(hmac_code).strip
data = {timestamp: , sign: }.merge(data)
end
res = post("#{API_CUSTOM_BOT_SEND}/#{hook_id}", data)
return nil if res.code != 200

JSON.parse(res.body)
Expand All @@ -375,7 +382,7 @@ def reserve_meeting(token, end_time, check_list, topic)
{
check_field: 1,
check_mode: 1,
check_list: check_list
check_list:
}
]
}
Expand Down Expand Up @@ -410,7 +417,7 @@ def update_reserve(token, reserve_id, topic, check_list, end_time)
{
check_field: 1,
check_mode: 1,
check_list: check_list
check_list:
}
]
}
Expand Down Expand Up @@ -470,7 +477,7 @@ def custom_robot_send_text(text, hook_id)
[
{
tag: 'text',
text: text
text:
}
]
]
Expand All @@ -481,7 +488,7 @@ def custom_robot_send_text(text, hook_id)
end

# 自定义机器人接口 发送卡片消息
def custom_robot_send_card(title = '标题', theme = 'blue', elements = [], hook_id = '')
def custom_robot_send_card(title = '标题', theme = 'blue', elements = [], hook_id = '', secret = '')
custom_robot_send({
msg_type: 'interactive',
card: {
Expand All @@ -495,9 +502,9 @@ def custom_robot_send_card(title = '标题', theme = 'blue', elements = [], hook
},
template: theme
},
elements: elements
elements:
}
}, hook_id)
}, hook_id, secret)
end
end
end

0 comments on commit 343599a

Please sign in to comment.