Skip to content

Commit

Permalink
Update documentation about commands
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann committed Jun 30, 2019
1 parent 314a39d commit 002079f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
36 changes: 36 additions & 0 deletions app/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,42 @@ def message_text(payload):
return True


@socketio.on('message_command')
@login_required
def message_command(payload):
current_user_id = current_user.get_id()
if not current_user_id:
return False, "invalid session id"
if not current_user.token.permissions.message_command:
return False, "insufficient rights"
if 'command' not in payload:
return False, 'missing argument: "msg"'
if 'room' not in payload:
return False, 'missing argument: "room"'

broadcast = payload.get('broadcast', False)
if broadcast and not current_user.token.permissions.message_broadcast:
return False, "insufficient rights"

room = Room.query.get(payload['room'])
if not room:
return False, 'Room not found'

user = {
'id': current_user_id,
'name': current_user.name,
}
emit('command', {
'command': payload['command'],
'user': user,
'timestamp': timegm(datetime.now().utctimetuple()),
}, room=room.name, broadcast=broadcast)
log_event("command", current_user, room, data={'command': payload['command']})
for room in current_user.rooms:
emit('stop_typing', {'user': user}, room=room.name)
return True


@socketio.on('image')
@login_required
def message_image(payload):
Expand Down
4 changes: 2 additions & 2 deletions app/static/js/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ function submit_private_image(receiver, url, width, height, resolve, reject) {
});
}

function submit_command(parameter, resolve, reject) {
socket.emit('command', { room: self_room, data: parameter }, (success, error) => {
function submit_command(command, resolve, reject) {
socket.emit('message_command', { room: self_room, command: command }, (success, error) => {
if (verify_query(success, error)) {
if (resolve !== undefined)
resolve();
Expand Down
2 changes: 1 addition & 1 deletion app/static/plugins/send-message.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if (text.match("^/")) {
submit_command(text.substr(1).split(' '));
submit_command(text.substr(1));
display_message(current_user, current_timestamp, text.substr(1), true);
} else if (text.match("^image:")) {
submit_image(text.substr(6), 300, 300, () => {
Expand Down
6 changes: 2 additions & 4 deletions docs/slurk_about.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ Technical concepts
events. For instance, they can be used to create rooms, connect or disconnect clients or generate tokens for logging
in.

**Commands** are a special kind of interaction with the server: Registered via the API, they can be used by human
users to control the bot, which in turn communicates with the server. Depending on the layout, commands are usually
prefixed with a slash as, for instance, in `/new_image_public`. In this example, the command triggers a change of what
is shown in the display area, visible to all clients in the current room.
**Commands** is basically the same as a public text message, but is only send to the users with the required
permissions.

**Tokens**: To provide control over who is allowed to log into the chat (since we're not interested in running a
public server here), access is regulated via tokens. Tokens need to be created in advance and link a user (who is
Expand Down
12 changes: 12 additions & 0 deletions docs/slurk_bots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,15 @@ If you want to change an image for example, you may use something like this:
'attribute': "src",
'value': url)
})
Commands
~~~~~~~~

Commands are very similar to text message, but requires a dedicated permission. Commands are sent to a room and a bot
can listen to a command with ``on_command``:

.. code-block:: python
def on_command(self, data):
do_something(data)

0 comments on commit 002079f

Please sign in to comment.