Ejemplo n.º 1
0
    def post(self):
        db = self.settings["db"]

        msg_dict  = parse_message(self.request.body)
        dict_id= yield insert_data(msg_dict)

        self.write("OK" if dict_id else "ERROR")
Ejemplo n.º 2
0
 def handle_message(self, stream):
     message = yield stream.read_until("\n".encode("ascii"))
     print("Mensaje recibido:", message)
     message_dict = parse_message(message)
     dict_id = yield self.insert_data(message_dict)
     response = "0\n" if dict_id else "1\n"
     yield stream.write(response.encode("ascii"))
Ejemplo n.º 3
0
	def test_parse_command_and_trailing(self):
		command = 'PING'
		trailing = 'zelazny.freenode.net'
		server_message = '%s :%s' % (command, trailing)
		message = parse.parse_message(server_message)

		self.assertEqual(message.command, command,
			'commands should match, expected "%s", got "%s"' % (command, message.command))		
		self.assertEqual(message.trailing, trailing,
			'trailings should match, expected "%s", got "%s"' % (trailing, message.trailing))
Ejemplo n.º 4
0
	def test_parse_all_but_prefix(self):
		command = '366'
		parameters = ['booby', '#markus_vs_warkus']
		trailing = 'End of /NAMES list.'
		server_message = '%s %s :%s' % (command, ' '.join(parameters), trailing)
		message = parse.parse_message(server_message)

		self.assertEqual(message.command, command,
			'commands should match, expected "%s", got "%s"' % (command, message.command))		
		self.assertEqual(message.parameters, parameters,
			'parameters should match, expected "%s", got "%s"' % (parameters, message.parameters))
		self.assertEqual(message.trailing, trailing,
			'trailings should match, expected "%s", got "%s"' % (trailing, message.trailing))
Ejemplo n.º 5
0
	def test_parse_all_but_trailing(self):
		prefix = 'zelazny.freenode.net'
		command = '353'
		parameters = ['booby', '@', '#markus_vs_warkus']
		server_message = ':%s %s %s' % (prefix, command, ' '.join(parameters))
		message = parse.parse_message(server_message)

		self.assertEqual(message.prefix, prefix, 
			'prefixes should match. expected: "%s", got "%s"' % (message.prefix, prefix))
		self.assertEqual(message.command, command,
			'commands should match, expected "%s", got "%s"' % (command, message.command))		
		self.assertEqual(message.parameters, parameters,
			'parameters should match, expected "%s", got "%s"' % (parameters, message.parameters))
Ejemplo n.º 6
0
	def test_parse_command_and_parameters(self):
		command = '353'
		parameters = ['booby', '@', '#markus_vs_warkus']
		trailing = '- Thank you for using freenode!'
		server_message = '%s %s :%s' % (command, ' '.join(parameters), trailing)
		message = parse.parse_message(server_message)


		self.assertEqual(message.command, command,
			'commands should match, expected "%s", got "%s"' % (command, message.command))		
		self.assertEqual(message.parameters, parameters,
			'parameters should match, expected "%s", got "%s"' % (parameters, message.parameters))
		self.assertEqual(message.trailing, trailing,
			'trailings should match, expected "%s", got "%s"' % (trailing, message.trailing))
Ejemplo n.º 7
0
	def test_parse_full_message(self):
		prefix = 'zelazny.freenode.net'
		command = '372'
		parameters = ['booby']
		trailing = '- Thank you for using freenode!'
		server_message = ':%s %s %s :%s' % (prefix, command, ''.join(parameters), trailing)
		message = parse.parse_message(server_message)

		self.assertEqual(message.prefix, prefix, 
			'prefixes should match. expected: "%s", got "%s"' % (message.prefix, prefix))
		self.assertEqual(message.command, command,
			'commands should match, expected "%s", got "%s"' % (command, message.command))		
		self.assertEqual(message.parameters, parameters,
			'parameters should match, expected "%s", got "%s"' % (parameters, message.parameters))
		self.assertEqual(message.trailing, trailing,
			'trailings should match, expected "%s", got "%s"' % (trailing, message.trailing))
Ejemplo n.º 8
0
	def get_messages(self):
		""" Receive messages from the server.

		The method receives data from the server through
		the connection and parses it into Message
		objects.

		Returns:
			List of parsed Message objects or None if there's
			network problems.
		"""
		data = self.connection.recv(4096)
		# FIXME(mk): is this if statement needed?
		if not data:
			return None

		messages = []
		raw_messages = parse.parse_messages_from(data)
		for raw_message in raw_messages:
			message = parse.parse_message(raw_message)
			if message:
				messages.append(message)

		return messages
Ejemplo n.º 9
0
def process_message_inner(config, message, conv):
    if not message.is_private_channel() \
            and message.bot_username != config.username \
            and not config.username in message.text \
            and not conv.is_strong_context():
        print "Ignoring message not for me"
        return False, None

    # TODO need some sort of onboarding for first-time user

    msg_type, data = parse.parse_message(message, conv, config)
    print "Received message parsed as " + str(msg_type) + " in context " + str(
        conv.context)
    if msg_type == parse.MSG_REMINDER and message.user().timezone is None:
        keybase.send(conv.id, ASSUME_TZ)
        message.user().set_timezone("US/Eastern")

    if msg_type == parse.MSG_REMINDER:
        reminder = data
        reminder.store()
        if not reminder.reminder_time:
            conv.set_context(conversation.CTX_WHEN, reminder=reminder)
            return keybase.send(conv.id, WHEN)
        else:
            conv.set_context(conversation.CTX_SET, reminder=reminder)
            return keybase.send(conv.id, reminder.confirmation())

    elif msg_type == parse.MSG_STFU:
        conv.clear_context()
        return keybase.send(conv.id, OK)

    elif msg_type == parse.MSG_HELP:
        message.user().set_seen_help()
        conv.clear_weak_context()
        return keybase.send(conv.id, HELP % config.owner)

    elif msg_type == parse.MSG_TIMEZONE:
        message.user().set_timezone(data)
        if conv.context == conversation.CTX_WHEN:
            return keybase.send(conv.id, ACK_WHEN)
        conv.clear_weak_context()
        return keybase.send(conv.id, ACK)

    elif msg_type == parse.MSG_WHEN:
        reminder = conv.get_reminder()
        reminder.set_time(data[0], data[1])
        confirmation = reminder.confirmation()
        conv.set_context(conversation.CTX_SET, reminder=reminder)
        return keybase.send(conv.id, confirmation)

    elif msg_type == parse.MSG_LIST:
        reminders = conv.get_all_reminders()
        conv.clear_weak_context()
        if not len(reminders):
            return keybase.send(conv.id, NO_REMINDERS)
        response = LIST_INTRO
        for i, reminder in enumerate(reminders, start=1):
            response += str(
                i) + ". " + reminder.body + " - " + reminder.human_time(
                    full=True) + "\n"
        return keybase.send(conv.id, response)

    elif msg_type == parse.MSG_UNDO:
        if conv.context == conversation.CTX_SET:
            conv.get_reminder().delete()
        elif conv.context == conversation.CTX_DELETED:
            conv.get_reminder().undelete()
        conv.clear_weak_context()
        return keybase.send(conv.id, OK)

    elif msg_type == parse.MSG_SOURCE:
        conv.clear_weak_context()
        return keybase.send(conv.id, SOURCE)

    elif msg_type == parse.MSG_UNKNOWN_TZ:
        conv.clear_weak_context()
        return keybase.send(conv.id, HELP_TZ)

    elif msg_type == parse.MSG_ACK:
        conv.clear_weak_context()
        return True, None

    elif msg_type == parse.MSG_GREETING:
        conv.clear_weak_context()
        return keybase.send(conv.id, data)

    elif msg_type == parse.MSG_DEBUG:
        conv.set_debug(True)
        return keybase.send(conv.id, DEBUG)

    elif msg_type == parse.MSG_NODEBUG:
        conv.set_debug(False)
        return keybase.send(conv.id, NODEBUG)

    elif msg_type == parse.MSG_DELETE:
        reminder = data
        reminder.delete()
        conv.set_context(conversation.CTX_DELETED, reminder)
        msg = "Alright, I've deleted the reminder to " + reminder.body + " that was set for " + \
            reminder.human_time(preposition=False) + "."
        return keybase.send(conv.id, msg)

    elif msg_type == parse.MSG_SNOOZE:
        if conv.context != conversation.CTX_REMINDED:
            return keybase.send(conv.id, "Not sure what to snooze.")
        conv.get_reminder().snooze_until(data.time)
        conv.set_context(conversation.CTX_SET, conv.get_reminder())
        return keybase.send(
            conv.id, "Ok. I'll remind you again in " + data.phrase + ".")

    elif msg_type == parse.MSG_UNKNOWN:
        # I don't think an unknown message should clear context at all
        #conv.clear_weak_context()
        if conv.debug:
            keybase.debug("Message from @" + message.user().name + " parsed UNKNOWN: " \
                    + message.text, config)
        if conv.context == conversation.CTX_WHEN:
            return True, HELP_WHEN
        else:  # CTX_NONE/weak
            if conv.is_recently_active() or message.user().has_seen_help:
                return True, UNKNOWN
            return True, PROMPT_HELP

    # Shouldn't be able to get here
    print msg_type, data
    assert False, "unexpected parsed msg_type"
Ejemplo n.º 10
0
from slackclient import SlackClient
import time
from parse import filter_message, parse_message, preprocess
from execute import execute_command
from config import SLACK_BOT_TOKEN, BOT_ID, READ_WEBSOCKET_DELAY

slack_client = SlackClient(SLACK_BOT_TOKEN)
if slack_client.rtm_connect():
    print("Bot connected and running!", flush=True)
    last_task = None
    while True:
        filtered = filter(filter_message, slack_client.rtm_read())
        for message in filtered:
            if message['text'] == '닫혀라 참깨': assert False
            try:
                print('message :', message)
                parsed_message = parse_message(message)
                parsed_message = preprocess(parsed_message, last_task)
                print('parsed message :', parsed_message)
                response, last_task = execute_command(parsed_message)
                print('response :', response)
            except Exception as e:
                print(e)
                response = "Sorry, I didn't quite get that. Type 'help' for instruction."
            slack_client.rtm_send_message(message['channel'], response)
        time.sleep(READ_WEBSOCKET_DELAY)
else:
    print("Connection failed. Invalid Slack token or bot ID?")
Ejemplo n.º 11
0
	def test_parse_non_existant_message(self):
		message = parse.parse_message('')
		self.assertEqual(message, None, "Message should be None")

		message = parse.parse_message(None)
		self.assertEqual(message, None, "Message should be None")