Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add message with sign #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
91 changes: 70 additions & 21 deletions lib/feishu-api/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ 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: )
end

def post_with_user_token(url, user_access_token, data, headers = {}, timeout = 30)
Expand All @@ -44,7 +51,28 @@ 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: )
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: )
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: )
end

def get_with_user_token(url, user_access_token, data = {}, headers = {}, timeout = 30)
Expand Down Expand Up @@ -72,7 +100,14 @@ 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: )
end

def delete_with_user_token(url, user_access_token, data = {}, headers = {}, timeout = 30)
Expand All @@ -86,7 +121,14 @@ 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: )
end

def patch_with_user_token(url, user_access_token, data = {}, headers = {}, timeout = 30)
Expand Down Expand Up @@ -128,7 +170,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 +184,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 +213,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 +228,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 +336,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 +359,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 +397,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 +424,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 +459,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 +519,7 @@ def custom_robot_send_text(text, hook_id)
[
{
tag: 'text',
text: text
text:
}
]
]
Expand All @@ -481,7 +530,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 +544,9 @@ def custom_robot_send_card(title = '标题', theme = 'blue', elements = [], hook
},
template: theme
},
elements: elements
elements:
}
}, hook_id)
}, hook_id, secret)
end
end
end