def reset_room(): """ Remove a room's messages --- tags: - room parameters: - in: query name: room schema: type: string responses: 200: description: Room schema: id: Message properties: room: type: string example: tutors description: the room that was reset """ room = request.values['room'] db = get_db() events.trigger_room_reset(db, room) return jsonify({'room': room})
def delete_bot(): """ Remove a Bot --- tags: - bots parameters: - in: query name: id schema: type: integer description: the ID of the bot to update, creates a new bot if no is ID given responses: 200: description: Returns the ID of the removed bot schema: properties: id: type: integer example: 1 description: the bot's unique ID """ id = request.values.get('id') db = get_db() bot = db.bots.remove(id) return jsonify({id: id})
def get_new_messages(): """ List messages --- tags: - messages parameters: - in: query name: room required: true schema: type: string description: the name of the room - in: query name: since schema: type: integer description: the ID of the last message you already have responses: 200: description: Messages since last message schema: type: array items: schema: id: Message properties: id: type: integer example: 1 description: the messages's unique ID room: type: string example: tutors description: the room the message was posted in author: type: string example: Georgina description: the author of the message text: type: string example: Hello, World! description: the message's text """ since_id = request.args.get('since') room = request.args.get('room') if room is None: return jsonify({'message': 'room name is required'}), 400 if since_id is not None: try: _ = int(since_id) except ValueError: return jsonify({'message': 'since_id must be an integer'}), 400 db = get_db() new_messages = list(db.messages.new(since_id, room=str(room))) return jsonify(new_messages)
def clear_room_state(): """ Clear any pending conversation state on a room --- tags: - room parameters: - in: query name: room schema: type: string responses: 200: description: Room schema: id: Message properties: room: type: string example: tutors description: the room that had its state cleared """ room = request.values['room'] db = get_db() events.trigger_clear_room_state(db, room) return jsonify({'room': room})
def delete_bot(): """ Remove a Bot --- tags: - bots parameters: - in: query name: id schema: type: integer description: ID of the bot to delete responses: 200: description: bot was successfully removed schema: properties: id: type: integer example: 1 description: ID of the removed bot """ id = request.args.get('id') if id is None: return jsonify({'message': 'id of a bot to remove is required'}), 400 db = get_db() found = db.bots.remove(id) if not found: return jsonify({'message': 'bot with this id not found'}), 404 return jsonify({'id': id})
def get_bots(): """ List Bots --- tags: - bots parameters: - in: query name: room schema: type: string description: The name of the room to list bots for. If not provided, will list all registered bots. responses: 200: description: Bots and their endpoints schema: type: array items: schema: id: Bot required: - name - responds_to - room - url properties: id: type: integer example: 1 description: the bot's unique ID name: type: string example: NeCSuS Bot description: the bot's name responds_to: type: string example: "(?P<greeting>hi|hello)(?P<other>.*)" description: regex that triggers sending the message to the bot room: type: string example: my_room description: room that the bot is registered in url: type: string example: https://necsus-bot.ncss.cloud description: the bot's url """ room = request.args.get('room') db = get_db() if room is not None: bots = list(db.bots.find_all(room=room)) return jsonify(bots) else: bots = list(db.bots.find_all()) return jsonify(bots)
def post_message(): """ Post a message to a room --- tags: - messages parameters: - in: body name: content required: true schema: type: object required: - room - author - text properties: room: type: string example: 'party-room' author: type: string nullable: true example: 'Kenni' text: type: string example: 'Hello sam' responses: 200: description: The message was successfully posted. schema: id: Message """ data = request.get_json() if not data: return jsonify({'message': 'message must be valid JSON object'}), 400 if not all([data.get('text'), data.get('room'), data.get('author')]): return jsonify({ 'message': 'text, room, and author keys must be present and non-empty' }), 400 db = get_db() message = { 'text': str(data['text']), 'room': str(data['room']), 'author': str(data['author']), } message_result = events.trigger_message_post(db, message) return jsonify(message_result)
def post_message(): """ Post a message to a room --- tags: - messages parameters: - in: body name: content required: true schema: type: object required: - room - author - text properties: room: type: string example: 'party-room' author: type: string nullable: true example: 'Kenni' text: type: string example: 'Hello sam' responses: 200: description: The message was successfully posted. schema: id: Message """ data = request.get_json() # room is allowed to be blank because "" is a room if not data or not data.get( 'text') or data.get('room') is None or not data.get('author'): return jsonify({'message': 'text, room, and author keys are required'}), 400 db = get_db() message = { 'text': str(data['text']), 'room': str(data['room']), 'author': str(data['author']), } message_result = events.trigger_message_post(db, message) return jsonify(message_result)
def get_new_messages(): """ List messages --- tags: - messages parameters: - in: query name: room schema: type: string description: the name of the room - in: query name: since schema: type: integer description: the ID of the last message you already have responses: 200: description: Messages since last message schema: id: Message properties: id: type: integer example: 1 description: the messages's unique ID room: type: string example: tutors description: the room the message was posted in author: type: string example: Georgina description: the author of the message text: type: string example: Hello, World! description: the message's text """ since_id = request.values.get('since') room = request.values.get('room') db = get_db() new_messages = list(db.messages.new(since_id, room=room)) return jsonify(new_messages)
def get_bots(): """ List Bots --- tags: - bots parameters: - in: query name: room schema: type: string description: the name of the room to list bots for responses: 200: description: Bots and their endpoints schema: type: array items: id: Bot properties: id: type: integer example: 1 description: the bot's unique ID name: type: string example: NeCSuS Bot description: the bot's name url: type: string example: https://necsus-bot.ncss.cloud description: the bot's url """ room = request.values.get('room') db = get_db() if room != None: bots = list(db.bots.find_all(room=room)) return jsonify(bots) else: bot = db.bots.list() return jsonify(bot)
def clear_room_messages(): """ Remove a room's messages --- tags: - room parameters: - in: body description: the room to clear name: content required: true schema: type: object required: - room properties: room: type: string example: my_room responses: 200: description: room messages successfully cleared schema: properties: room: type: string example: tutors description: the room that was cleared """ data = request.get_json() if not data or data.get('room') is None: return jsonify({'message': 'room name is required'}), 400 db = get_db() room = events.trigger_clear_room_messages(db, str(data.get('room'))) return jsonify({'room': room})
def post_bot(): """ Create or Update a Bot --- tags: - bots parameters: - in: body name: content required: true schema: id: Bot responses: 200: description: The created or updated bot schema: id: Bot """ data = request.get_json() # TODO: verify these; make nicer behaviour if bot with supplied id exists, # reject if will create new bot and missing fields. room = data.get('room') id = data.get('id') name = data.get('name') responds_to = data.get('responds_to') url = data.get('url') db = get_db() bot = db.bots.update_or_add(id=id, room=room, name=name, responds_to=responds_to, url=url) return jsonify(bot)
def post_message(): """ Post a Bot --- tags: - messages parameters: - in: query name: room schema: type: string description: the name of the room to post the message - in: query name: author schema: type: string description: the name of the message's author - in: query name: text schema: type: string description: the name of the message's text responses: 200: description: The message was successfully posted. schema: id: Message """ if request.values['text'].strip() == "": return jsonify({}), 400 db = get_db() message = dict(request.values) message_result = events.trigger_message_post(db, message) return jsonify(message_result)
def post_bot(): """ Create or Update a Bot --- tags: - bots parameters: - in: query name: id schema: type: integer description: the ID of the bot to update, creates a new bot if no is ID given - in: query name: room schema: type: string description: the name of the room the bot is in, the bot's room is not updated if no room parameter is given - in: query name: name schema: type: string description: the name of the bot, the bot's name is not updated if no url parameter is given - in: query name: url schema: type: string description: the URL endpoint for the bot, the bot's url is not updated if no url parameter is given responses: 200: description: Returns a bot schema: id: Bot properties: id: type: integer example: 1 description: the bot's unique ID name: type: string example: Repeat Bot description: the bot's name responds_to: type: string example: repeat (?P<word>\w+) (?P<count>\d+) times description: the regular expression that the bot listens for in messages url: type: string example: https://repeat-bot.kennib.repl.co description: the bot's url """ room = request.values.get('room') id = request.values.get('id') name = request.values.get('name') responds_to = request.values.get('responds_to') url = request.values.get('url') db = get_db() bot = db.bots.update_or_add(id=id, room=room, name=name, responds_to=responds_to, url=url) return jsonify(bot)