Module: FeishuApi

Defined in:
lib/feishu-api.rb,
lib/feishu-api/api.rb,
lib/feishu-api/config.rb,
lib/feishu-api/engine.rb,
lib/feishu-api/version.rb

Defined Under Namespace

Classes: Config, Engine

Constant Summary collapse

API_HOST =
'https://open.feishu.cn/open-apis'
API_TENANT_ACCESS_TOKEN =
'/auth/v3/tenant_access_token/internal'
API_APP_ACCESS_TOKEN =
'/auth/v3/app_access_token/internal'
API_SEND_MESSAGES =
'/im/v1/messages'
API_CUSTOM_BOT_SEND =
'/bot/v2/hook'
VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.api(interface) ⇒ Object



12
13
14
# File 'lib/feishu-api/api.rb', line 12

def api(interface)
  "#{API_HOST}#{interface}"
end

.app_access_tokenObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/feishu-api/api.rb', line 46

def app_access_token
  Rails.cache.fetch('app_access_token', expires_in: 2.hours) do
    res = post(API_APP_ACCESS_TOKEN, {
                 app_id: Config.app_id,
                 app_secret: Config.app_secret
               })
    return nil if res.code != 200

    body = JSON.parse(res.body)
    token = (body['code']).zero? ? body['app_access_token'] : nil
    token
  end
end

.configure(&block) ⇒ Object



12
13
14
# File 'lib/feishu-api.rb', line 12

def configure(&block)
  Config.configure(&block)
end

.custom_robot_send(data, hook_id) ⇒ Object

自定义机器人接口 发送消息



85
86
87
88
89
90
# File 'lib/feishu-api/api.rb', line 85

def custom_robot_send(data, hook_id)
  post("#{API_CUSTOM_BOT_SEND}/#{hook_id}", data)
  return nil if res.code != 200

  JSON.parse(res.body)
end

.custom_robot_send_card(title = '标题', theme = 'blue', elements = [], hook_id = '') ⇒ Object

自定义机器人接口 发送卡片消息



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/feishu-api/api.rb', line 115

def custom_robot_send_card(title = '标题', theme = 'blue', elements = [], hook_id = '')
  custom_robot_send({
                      msg_type: 'interactive',
                      card: {
                        config: {
                          wide_screen_mode: true
                        },
                        header: {
                          title: {
                            tag: 'plain_text',
                            content: title
                          },
                          template: theme
                        },
                        elements: elements
                      }
                    }, hook_id)
end

.custom_robot_send_text(text, hook_id) ⇒ Object

自定义机器人接口 发送文本消息



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/feishu-api/api.rb', line 93

def custom_robot_send_text(text, hook_id)
  custom_robot_send({
                      msg_type: 'post',
                      content: {
                        post: {
                          'zh-CN': {
                            title: '',
                            content: [
                              [
                                {
                                  tag: 'text',
                                  text: text
                                }
                              ]
                            ]
                          }
                        }
                      }
                    }, hook_id)
end

.post(url, data, headers = {}, timeout = 30) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/feishu-api/api.rb', line 16

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,
                verify: false)
end

.post_with_token(url, data, _timeout = 30) ⇒ Object



24
25
26
27
28
29
# File 'lib/feishu-api/api.rb', line 24

def post_with_token(url, data, _timeout = 30)
  headers = { Authorization: "Bearer #{tenant_access_token}" }
  post(url,
       data,
       headers)
end

.send_message(receive_type, receive_id, msg_type, content) ⇒ Object

发送消息



61
62
63
64
65
66
67
# File 'lib/feishu-api/api.rb', line 61

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 })
  return nil if res.code != 200

  JSON.parse(res.body)
end

.send_message_by_email(receive_id, msg_type, content) ⇒ Object

通过邮箱识别用户, 发消息到指定用户



80
81
82
# File 'lib/feishu-api/api.rb', line 80

def send_message_by_email(receive_id, msg_type, content)
  send_message('email', receive_id, msg_type, content)
end

.send_message_by_group(receive_id, msg_type, content) ⇒ Object

发消息到指定群聊



70
71
72
# File 'lib/feishu-api/api.rb', line 70

def send_message_by_group(receive_id, msg_type, content)
  send_message('chat_id', receive_id, msg_type, content)
end

.send_text_by_group(receive_id, text) ⇒ Object

发文本消息到指定群聊



75
76
77
# File 'lib/feishu-api/api.rb', line 75

def send_text_by_group(receive_id, text)
  send_message_by_group(receive_id, 'text', JSON.generate({ text: text }))
end

.tenant_access_tokenObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/feishu-api/api.rb', line 31

def tenant_access_token
  Rails.cache.fetch('tenant_access_token', expires_in: 2.hours) do
    res = post(API_TENANT_ACCESS_TOKEN, {
                 app_id: Config.app_id,
                 app_secret: Config.app_secret
               })

    return nil if res.code != 200

    body = JSON.parse(res.body)
    token = (body['code']).zero? ? body['tenant_access_token'] : nil
    token
  end
end