Module: MixinBot::API::Blaze

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

Instance Method Summary collapse

Instance Method Details

#blazeObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/mixin_bot/api/blaze.rb', line 6

def blaze
  access_token = access_token('GET', '/', '')

  authorization = format('Bearer %<access_token>s', access_token:)
  Faye::WebSocket::Client.new(
    format('wss://%<host>s/', host: config.blaze_host),
    ['Mixin-Blaze-1'],
    headers: { 'Authorization' => authorization },
    ping: 60
  )
end

#blaze_send_app_button(socket, conversation_id:, recipient_id:, label:, action:, color:) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mixin_bot/api/blaze.rb', line 117

def blaze_send_app_button(socket, conversation_id:, recipient_id:, label:, action:, color:)
  data = [{ label:, action:, color: }].to_json
  socket.send write_ws_message(
    params: {
      conversation_id:,
      recipient_id:,
      message_id: SecureRandom.uuid,
      category: 'APP_BUTTON_GROUP',
      data_base64: Base64.urlsafe_encode64(data, padding: false)
    }
  )
end

#blaze_send_app_card(socket, conversation_id:, recipient_id:, title:, description:, action:, icon_url:) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mixin_bot/api/blaze.rb', line 104

def blaze_send_app_card(socket, conversation_id:, recipient_id:, title:, description:, action:, icon_url:)
  data = { title:, description:, action:, icon_url: }.to_json
  socket.send write_ws_message(
    params: {
      conversation_id:,
      recipient_id:,
      message_id: SecureRandom.uuid,
      category: 'APP_CARD',
      data_base64: Base64.urlsafe_encode64(data, padding: false)
    }
  )
end

#blaze_send_contact(socket, conversation_id:, recipient_id:, contact_id:) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mixin_bot/api/blaze.rb', line 91

def blaze_send_contact(socket, conversation_id:, recipient_id:, contact_id:)
  data = { user_id: contact_id }.to_json
  socket.send write_ws_message(
    params: {
      conversation_id:,
      recipient_id:,
      message_id: SecureRandom.uuid,
      category: 'PLAIN_CONTACT',
      data_base64: Base64.urlsafe_encode64(data, padding: false)
    }
  )
end

#blaze_send_group_app_button(socket, conversation_id:, recipient_id:, buttons:) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mixin_bot/api/blaze.rb', line 130

def blaze_send_group_app_button(socket, conversation_id:, recipient_id:, buttons:)
  data = buttons.to_json
  socket.send write_ws_message(
    params: {
      conversation_id:,
      recipient_id:,
      message_id: SecureRandom.uuid,
      category: 'APP_BUTTON_GROUP',
      data_base64: Base64.urlsafe_encode64(data, padding: false)
    }
  )
end

#blaze_send_plain_text(socket, conversation_id:, recipient_id:, content:) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mixin_bot/api/blaze.rb', line 62

def blaze_send_plain_text(socket, conversation_id:, recipient_id:, content:)
  socket.send write_ws_message(
    params: {
      conversation_id:,
      recipient_id:,
      message_id: SecureRandom.uuid,
      category: 'PLAIN_TEXT',
      data_base64: Base64.urlsafe_encode64(content.to_s, padding: false)
    }
  )
end

#blaze_send_post(socket, conversation_id:, recipient_id:, content:) ⇒ Object



87
88
89
# File 'lib/mixin_bot/api/blaze.rb', line 87

def blaze_send_post(socket, conversation_id:, recipient_id:, content:)
  blaze_send_plain_text(socket, conversation_id:, recipient_id:, content:)
end

#blaze_send_recall_message(socket, conversation_id:, recipient_id:, message_id:) ⇒ Object



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

def blaze_send_recall_message(socket, conversation_id:, recipient_id:, message_id:)
  data = { message_id: }.to_json
  socket.send write_ws_message(
    params: {
      conversation_id:,
      recipient_id:,
      message_id: SecureRandom.uuid,
      category: 'MESSAGE_RECALL',
      data_base64: Base64.urlsafe_encode64(data, padding: false)
    }
  )
end

#start_blaze_connect(reconnect: true, &_block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mixin_bot/api/blaze.rb', line 18

def start_blaze_connect(reconnect: true, &_block)
  ws ||= blaze
  yield if block_given?

  ws.on :open do |event|
    if defined? on_open
      on_open ws, event
    else
      p [Time.now.to_s, :open]
      ws.send list_pending_message
    end
  end

  ws.on :message do |event|
    if defined? on_message
      on_message ws, event
    else
      raw = JSON.parse ws_message(event.data)
      p [Time.now.to_s, :message, raw&.[]('action')]

      ws.send acknowledge_message_receipt(raw['data']['message_id']) unless raw&.[]('data')&.[]('message_id').nil?
    end
  end

  ws.on :error do |event|
    if defined? on_error
      on_error ws, event
    else
      p [Time.now.to_s, :error]
    end
  end

  ws.on :close do |event|
    if defined? on_close
      on_close ws, event
    else
      p [Time.now.to_s, :close, event.code, event.reason]
    end

    ws = nil
    start_blaze_connect(&_block) if reconnect
  end
end