Module: Keybase::Local::Chat

Defined in:
lib/keybase/local/chat.rb

Overview

Represents Keybase's JSON chat API.

Constant Summary collapse

CHAT_EXEC_ARGS =

The initial arguments to pass when executing Keybase for chatting.

%w[keybase chat api].freeze

Class Method Summary collapse

Class Method Details

.chat_call(meth, options: {}) ⇒ OpenStruct

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Makes chat API calls.

Parameters:

  • meth (String, Symbol)

    the team method

  • options (Hash) (defaults to: {})

    the options hash

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



45
46
47
48
49
50
51
52
53
# File 'lib/keybase/local/chat.rb', line 45

def chat_call(meth, options: {})
  response = Open3.popen3(*CHAT_EXEC_ARGS) do |stdin, stdout, _, _|
    stdin.write envelope meth, options: options
    stdin.close
    stdout.read
  end

  unwrap JSON.parse response, object_class: OpenStruct
end

.conversation(users, peek: false, unread_only: false) ⇒ OpenStruct

Read a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • peek (Boolean) (defaults to: false)

    whether to mark the conversation read

  • unread_only (Boolean) (defaults to: false)

    whether to fetch unread messages only

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



71
72
73
74
75
76
77
78
79
# File 'lib/keybase/local/chat.rb', line 71

def conversation(users, peek: false, unread_only: false)
  chat_call :read, options: {
    channel: {
      name: Core::U[*users],
    },
    peek: peek,
    unread_only: unread_only,
  }
end

.delete_message(users, id) ⇒ OpenStruct

Delete a message from a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • id (Integer)

    the id of the message to delete

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



130
131
132
133
134
135
136
137
# File 'lib/keybase/local/chat.rb', line 130

def delete_message(users, id)
  chat_call :delete, options: {
    channel: {
      name: Core::U[*users],
    },
    message_id: id,
  }
end

.download_attachment(users, id, path) ⇒ OpenStruct

Download a file from a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • id (Integer)

    the id of the message to download from

  • path (String)

    the pathname to download to

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



179
180
181
182
183
184
185
186
187
# File 'lib/keybase/local/chat.rb', line 179

def download_attachment(users, id, path)
  chat_call :download, options: {
    channel: {
      name: Core::U[*users],
    },
    message_id: id,
    output: path,
  }
end

.edit_message(users, id, message) ⇒ OpenStruct

Edit a message in a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • id (Integer)

    the id of the message to delete

  • message (String)

    the message to send

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/keybase/local/chat.rb', line 145

def edit_message(users, id, message)
  chat_call :edit, options: {
    channel: {
      name: Core::U[*users],
    },
    message_id: id,
    message: {
      body: message,
    },
  }
end

.envelope(meth, options: {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the JSON serialized envelope.

Parameters:

  • meth (Symbol)

    the chat method

  • options (Hash) (defaults to: {})

    the options hash

Returns:

  • (String)

    the JSON serialized envelope



19
20
21
22
23
24
25
26
# File 'lib/keybase/local/chat.rb', line 19

def envelope(meth, options: {})
  {
    method: meth,
    params: {
      options: options,
    },
  }.to_json
end

.list_inbox(topic_type: nil) ⇒ OpenStruct

List the current user's inbox.

Parameters:

  • topic_type (String) (defaults to: nil)

    the topic type to list by

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



59
60
61
62
63
# File 'lib/keybase/local/chat.rb', line 59

def list_inbox(topic_type: nil)
  chat_call :list, options: {
    topic_type: topic_type,
  }
end

.mark_conversation(users, id) ⇒ OpenStruct

Make a conversation as read up to a specific ID.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • id (Integer)

    the id of the message to mark up to

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



194
195
196
197
198
199
200
201
# File 'lib/keybase/local/chat.rb', line 194

def mark_conversation(users, id)
  chat_call :mark, options: {
    channel: {
      name: Core::U[*users],
    },
    message_id: id,
  }
end

.mute_conversation(users) ⇒ OpenStruct

Mute a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



207
208
209
210
211
212
213
214
# File 'lib/keybase/local/chat.rb', line 207

def mute_conversation(users)
  chat_call :setstatus, options: {
    channel: {
      name: Core::U[*users],
    },
    status: "muted",
  }
end

.send_message(users, message, public: false, exploding_lifetime: nil) ⇒ OpenStruct

Send a message to a conversation. For team-based conversations, see send_team_message.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • message (String)

    the message to send

  • public (Boolean) (defaults to: false)

    whether to send the message to a public channel

  • exploding_lifetime (String, nil) (defaults to: nil)

    how long to wait before exploding the message, or nil to not explode it

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/keybase/local/chat.rb', line 89

def send_message(users, message, public: false, exploding_lifetime: nil)
  chat_call :send, options: {
    channel: {
      name: Core::U[*users],
      public: public,
    },
    exploding_lifetime: exploding_lifetime,
    message: {
      body: message,
    },
  }
end

.send_team_message(team, topic, message, exploding_lifetime: nil) ⇒ OpenStruct

Send a message to a team-based conversation. For conversations between individual users, see send_message.

Parameters:

  • team (String)

    the name of the team to which the conversation belongs

  • topic (String)

    the conversation's topic

  • message (String)

    the message to send

  • exploding_lifetime (String, nil) (defaults to: nil)

    how long to wait before exploding the message, or nil to not explode it

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/keybase/local/chat.rb', line 111

def send_team_message(team, topic, message, exploding_lifetime: nil)
  chat_call :send, options: {
    channel: {
      name: team,
      members_type: "team",
      topic_name: topic,
    },
    exploding_lifetime: exploding_lifetime,
    message: {
      body: message,
    },
  }
end

.unwrap(struct) ⇒ OpenStruct

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Cleans up the object returned by chat_call.

Parameters:

  • struct (OpenStruct)

    a structified response from the Keybase chat API

Returns:

  • (OpenStruct)

    an unwrapped version of the response

Raises:



33
34
35
36
37
# File 'lib/keybase/local/chat.rb', line 33

def unwrap(struct)
  raise Exceptions::ChatError, struct.error.message if struct.error

  struct.result
end

.upload_attachment(users, path, title) ⇒ OpenStruct

Upload a file to a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • path (String)

    the pathname of the file to upload

  • title (String)

    the uploaded file's title

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



163
164
165
166
167
168
169
170
171
# File 'lib/keybase/local/chat.rb', line 163

def upload_attachment(users, path, title)
  chat_call :attach, options: {
    channel: {
      name: Core::U[*users],
    },
    filename: path,
    title: title,
  }
end