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_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

#app_button_group(options) ⇒ Object



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

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

#app_card(options) ⇒ Object



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

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

#base_message_params(options) ⇒ Object

base format of message params



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

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_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



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

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

#recall_message_params(message_id, options) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mixin_bot/api/message.rb', line 59

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



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

def send_app_button_group_message(options)
  send_message app_button_group(options)
end

#send_app_card_message(options) ⇒ Object



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

def send_app_card_message(options)
  send_message app_card(options)
end

#send_contact_message(options) ⇒ Object



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

def send_contact_message(options)
  send_message plain_contact(options)
end

#send_file_message(options) ⇒ Object



124
125
126
# File 'lib/mixin_bot/api/message.rb', line 124

def send_file_message(options)
  send_message plain_data(options)
end

#send_image_message(options) ⇒ Object



120
121
122
# File 'lib/mixin_bot/api/message.rb', line 120

def send_image_message(options)
  send_message plain_image(options)
end

#send_message(payload) ⇒ Object

http post request



153
154
155
156
157
158
159
160
161
# File 'lib/mixin_bot/api/message.rb', line 153

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



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

def send_plain_messages(messages)
  send_message messages
end

#send_post_message(options) ⇒ Object



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

def send_post_message(options)
  send_message plain_post(options)
end

#send_text_message(options) ⇒ Object

use HTTP to send message



116
117
118
# File 'lib/mixin_bot/api/message.rb', line 116

def send_text_message(options)
  send_message plain_text(options)
end

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

gzip the message for websocket



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

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

  io = StringIO.new 'wb'
  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



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

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