Ejemplo n.º 1
0
	def process_request(query, channel):
		"""
		Each user query is forwarded to the appropriate function based on the command in the query.
		Once the query has been processed, the resulting response is sent as a message to the channel. 
		If no commands are found, the default is to fetch the last price for the ticker.
		"""
		# components - each query split into a list of words and symbols
		components = query.split(' ')
		if '' in components: 
			components.remove('')
		ticker = components[0]
		if len(components)>1:
			command = list(set(COMMANDS).intersection(components[1:]))
			if not command: 
				message = Response.unknown_command(ticker)
				return slack_client.api_call("chat.postMessage", channel=channel, text=message, as_user=True)
			components = components[2:]
			output = OPERATIONS[command[0]](ticker, components)
			message = output["message"]
			if output.get("attachments"):
				attachments = output['attachments']
				return slack_client.api_call("chat.postMessage", channel=channel, text=message, attachments=attachments, as_user=True)
			return slack_client.api_call("chat.postMessage", channel=channel, text=message, as_user=True)
		message = OPERATIONS["last_price"](ticker)
		return slack_client.api_call("chat.postMessage", channel=channel, text=message, as_user=True)