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



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

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

#app_card(options) ⇒ Object



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

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

#base_message_params(options) ⇒ Object

base format of message params



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mixin_bot/api/message.rb', line 82

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



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

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

#plain_contact(options) ⇒ Object



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

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

#plain_data(options) ⇒ Object



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

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

#plain_image(options) ⇒ Object



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

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

#plain_post(options) ⇒ Object



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

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

#plain_sticker(options) ⇒ Object



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

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

#plain_text(options) ⇒ Object



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

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

#plain_video(options) ⇒ Object



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

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

#recall_message(message_id, options) ⇒ Object



150
151
152
# File 'lib/mixin_bot/api/message.rb', line 150

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

#recall_message_params(message_id, options) ⇒ Object



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

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

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

#send_app_button_group_message(options) ⇒ Object



146
147
148
# File 'lib/mixin_bot/api/message.rb', line 146

def send_app_button_group_message(options)
  send_message app_button_group(options)
end

#send_app_card_message(options) ⇒ Object



142
143
144
# File 'lib/mixin_bot/api/message.rb', line 142

def send_app_card_message(options)
  send_message app_card(options)
end

#send_contact_message(options) ⇒ Object



138
139
140
# File 'lib/mixin_bot/api/message.rb', line 138

def send_contact_message(options)
  send_message plain_contact(options)
end

#send_file_message(options) ⇒ Object



130
131
132
# File 'lib/mixin_bot/api/message.rb', line 130

def send_file_message(options)
  send_message plain_data(options)
end

#send_image_message(options) ⇒ Object



126
127
128
# File 'lib/mixin_bot/api/message.rb', line 126

def send_image_message(options)
  send_message plain_image(options)
end

#send_message(payload) ⇒ Object

http post request



159
160
161
162
163
164
165
166
167
# File 'lib/mixin_bot/api/message.rb', line 159

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



154
155
156
# File 'lib/mixin_bot/api/message.rb', line 154

def send_plain_messages(messages)
  send_message messages
end

#send_post_message(options) ⇒ Object



134
135
136
# File 'lib/mixin_bot/api/message.rb', line 134

def send_post_message(options)
  send_message plain_post(options)
end

#send_text_message(options) ⇒ Object

use HTTP to send message



122
123
124
# File 'lib/mixin_bot/api/message.rb', line 122

def send_text_message(options)
  send_message plain_text(options)
end

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

gzip the message for websocket



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mixin_bot/api/message.rb', line 107

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



97
98
99
100
101
102
103
104
# File 'lib/mixin_bot/api/message.rb', line 97

def ws_message(data)
  io = StringIO.new(data.pack('c*'), 'rb')
  gzip = Zlib::GzipReader.new io
  msg = gzip.read
  gzip.close

  msg
end