Module: MixinBot::API::Message

Included in:
MixinBot::API
Defined in:
lib/mixin_bot/api/message.rb

Overview

Instance Method Summary collapse

Instance Method Details

#acknowledge_message(message_id, status: 'READ', access_token: nil) ⇒ Object Also known as: send_acknowledgement



188
189
190
191
# File 'lib/mixin_bot/api/message.rb', line 188

def acknowledge_message(message_id, status: 'READ', access_token: nil)
  payload = { message_id:, status: }
  client.post '/acknowledgements', payload, access_token:
end

#acknowledge_message_receipt(message_id) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/mixin_bot/api/message.rb', line 11

def acknowledge_message_receipt(message_id)
  params = {
    message_id:,
    status: 'READ'
  }
  write_ws_message(action: 'ACKNOWLEDGE_MESSAGE_RECEIPT', params:)
end

#acknowledge_messages(messages, access_token: nil) ⇒ Object Also known as: send_acknowledgements



194
195
196
# File 'lib/mixin_bot/api/message.rb', line 194

def acknowledge_messages(messages, access_token: nil)
  client.post '/acknowledgements', *messages, access_token:
end

#app_button_group(options) ⇒ Object



67
68
69
# File 'lib/mixin_bot/api/message.rb', line 67

def app_button_group(options)
  base_message_params(options.merge(category: 'APP_BUTTON_GROUP'))
end

#app_card(options) ⇒ Object



63
64
65
# File 'lib/mixin_bot/api/message.rb', line 63

def app_card(options)
  base_message_params(options.merge(category: 'APP_CARD'))
end

#base_message_params(options) ⇒ Object

base format of message params



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mixin_bot/api/message.rb', line 85

def base_message_params(options)
  data = options[:data].is_a?(String) ? options[:data] : options[:data].to_json
  {
    conversation_id: options[:conversation_id],
    recipient_id: options[:recipient_id],
    representative_id: options[:representative_id],
    category: options[:category],
    status: 'SENT',
    quote_message_id: options[:quote_message_id],
    message_id: options[:message_id] || SecureRandom.uuid,
    data: Base64.encode64(data)
  }.compact
end

#list_pending_messageObject



7
8
9
# File 'lib/mixin_bot/api/message.rb', line 7

def list_pending_message
  write_ws_message(action: 'LIST_PENDING_MESSAGES', params: {})
end

#plain_audio(options) ⇒ Object



43
44
45
# File 'lib/mixin_bot/api/message.rb', line 43

def plain_audio(options)
  base_message_params(options.merge(category: 'PLAIN_AUDIO'))
end

#plain_contact(options) ⇒ Object



39
40
41
# File 'lib/mixin_bot/api/message.rb', line 39

def plain_contact(options)
  base_message_params(options.merge(category: 'PLAIN_CONTACT'))
end

#plain_data(options) ⇒ Object



31
32
33
# File 'lib/mixin_bot/api/message.rb', line 31

def plain_data(options)
  base_message_params(options.merge(category: 'PLAIN_DATA'))
end

#plain_image(options) ⇒ Object



27
28
29
# File 'lib/mixin_bot/api/message.rb', line 27

def plain_image(options)
  base_message_params(options.merge(category: 'PLAIN_IMAGE'))
end

#plain_live(options) ⇒ Object



51
52
53
# File 'lib/mixin_bot/api/message.rb', line 51

def plain_live(options)
  base_message_params(options.merge(category: 'PLAIN_LIVE'))
end

#plain_location(options) ⇒ Object



55
56
57
# File 'lib/mixin_bot/api/message.rb', line 55

def plain_location(options)
  base_message_params(options.merge(category: 'PLAIN_LOCATION'))
end

#plain_post(options) ⇒ Object



23
24
25
# File 'lib/mixin_bot/api/message.rb', line 23

def plain_post(options)
  base_message_params(options.merge(category: 'PLAIN_POST'))
end

#plain_sticker(options) ⇒ Object



35
36
37
# File 'lib/mixin_bot/api/message.rb', line 35

def plain_sticker(options)
  base_message_params(options.merge(category: 'PLAIN_STICKER'))
end

#plain_text(options) ⇒ Object



19
20
21
# File 'lib/mixin_bot/api/message.rb', line 19

def plain_text(options)
  base_message_params(options.merge(category: 'PLAIN_TEXT'))
end

#plain_video(options) ⇒ Object



47
48
49
# File 'lib/mixin_bot/api/message.rb', line 47

def plain_video(options)
  base_message_params(options.merge(category: 'PLAIN_VIDEO'))
end

#recall_message(message_id, options) ⇒ Object



180
181
182
# File 'lib/mixin_bot/api/message.rb', line 180

def recall_message(message_id, options)
  send_message [recall_message_params(message_id, options)]
end

#recall_message_params(message_id, options) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mixin_bot/api/message.rb', line 71

def recall_message_params(message_id, options)
  raise 'recipient_id is required!' if options[:recipient_id].nil?

  base_message_params(
    options.merge(
      category: 'MESSAGE_RECALL',
      data: {
        message_id:
      }
    )
  )
end

#send_app_button_group_message(options) ⇒ Object



152
153
154
# File 'lib/mixin_bot/api/message.rb', line 152

def send_app_button_group_message(options)
  send_message app_button_group(options)
end

#send_app_card_message(options) ⇒ Object



148
149
150
# File 'lib/mixin_bot/api/message.rb', line 148

def send_app_card_message(options)
  send_message app_card(options)
end

#send_audio_message(options) ⇒ Object



160
161
162
# File 'lib/mixin_bot/api/message.rb', line 160

def send_audio_message(options)
  send_message plain_audio(options)
end

#send_contact_message(options) ⇒ Object



144
145
146
# File 'lib/mixin_bot/api/message.rb', line 144

def send_contact_message(options)
  send_message plain_contact(options)
end

#send_file_message(options) ⇒ Object



136
137
138
# File 'lib/mixin_bot/api/message.rb', line 136

def send_file_message(options)
  send_message plain_data(options)
end

#send_image_message(options) ⇒ Object



132
133
134
# File 'lib/mixin_bot/api/message.rb', line 132

def send_image_message(options)
  send_message plain_image(options)
end

#send_live_message(options) ⇒ Object



168
169
170
# File 'lib/mixin_bot/api/message.rb', line 168

def send_live_message(options)
  send_message plain_live(options)
end

#send_location_message(options) ⇒ Object



172
173
174
# File 'lib/mixin_bot/api/message.rb', line 172

def send_location_message(options)
  send_message plain_location(options)
end

#send_message(payload) ⇒ Object

http post request



200
201
202
203
204
205
206
207
208
# File 'lib/mixin_bot/api/message.rb', line 200

def send_message(payload)
  path = '/messages'

  if payload.is_a? Hash
    client.post path, **payload
  elsif payload.is_a? Array
    client.post path, *payload
  end
end

#send_plain_messages(messages) ⇒ Object



184
185
186
# File 'lib/mixin_bot/api/message.rb', line 184

def send_plain_messages(messages)
  send_message messages
end

#send_post_message(options) ⇒ Object



140
141
142
# File 'lib/mixin_bot/api/message.rb', line 140

def send_post_message(options)
  send_message plain_post(options)
end

#send_sticker_message(options) ⇒ Object



156
157
158
# File 'lib/mixin_bot/api/message.rb', line 156

def send_sticker_message(options)
  send_message plain_sticker(options)
end

#send_text_message(options) ⇒ Object

use HTTP to send message



128
129
130
# File 'lib/mixin_bot/api/message.rb', line 128

def send_text_message(options)
  send_message plain_text(options)
end

#send_transfer_message(options) ⇒ Object



176
177
178
# File 'lib/mixin_bot/api/message.rb', line 176

def send_transfer_message(options)
  send_message transfer_message(options)
end

#send_video_message(options) ⇒ Object



164
165
166
# File 'lib/mixin_bot/api/message.rb', line 164

def send_video_message(options)
  send_message plain_video(options)
end

#transfer_message(options) ⇒ Object



59
60
61
# File 'lib/mixin_bot/api/message.rb', line 59

def transfer_message(options)
  base_message_params(options.merge(category: 'SYSTEM_ACCOUNT_SNAPSHOT'))
end

#write_ws_message(params:, action: 'CREATE_MESSAGE') ⇒ Object

gzip the message for websocket



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mixin_bot/api/message.rb', line 113

def write_ws_message(params:, action: 'CREATE_MESSAGE')
  msg = {
    id: SecureRandom.uuid,
    action:,
    params:
  }.to_json

  io = StringIO.new
  gzip = Zlib::GzipWriter.new io
  gzip.write msg
  gzip.close
  io.string.unpack('c*')
end

#ws_message(data) ⇒ Object

read the gzipped message form websocket



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mixin_bot/api/message.rb', line 100

def ws_message(data)
  data = data.pack('c*') if data.is_a?(Array)
  raise MixinBot::ArgumentError, 'data should be String or Array of integer' unless data.is_a?(String)

  io = StringIO.new(data, 'rb')
  gzip = Zlib::GzipReader.new io
  msg = gzip.read
  gzip.close

  msg
end