import time from slackclient import SlackClient # TODO: 1. Die beiden Zeilen müssen angepasst werden: BOT_ID = 'U5K1U7CPR' slack_client = SlackClient('xoxb-189062250807-7hxGFCt5elS1b5p6BhuB5Re8') # TODO: 2. Ändere den Namen deines Chatbots BOT_NAME = 'ChrisBot' # TODO: 3. Ändere den ersten Satz des Chatbots nach Betreten des Raums starting_sentence = 'Hi there' def find_response(received_message): response = "" # TODO: 4. Passe die Regeln an. Reguläre Ausdrücke, externe Quellen und Bibliotheken können auch eingebunden werden. # Rule-based answers if received_message.startswith('do'): response = 'Yes, I do!' elif received_message.startswith('why'): response = "Because... I don't know" elif received_message.startswith('how'): response = "Horrible" elif received_message.startswith('hi'): response = "Long time no see" elif received_message.startswith('when'): response = "Always!" return response # region DoNotChange # Ab hier keine Zeile ändern! # BOT_NAME_l = BOT_NAME.lower() AT_BOT = "<@" + BOT_ID + ">" AT_BOT_l = AT_BOT.lower() def handle_command(received_message, on_channel): """ Receives commands directed at the bot and determines if they are valid commands. If so, then acts on the commands. If not, returns back what it needs for clarification. """ # Prevents Bots to talk to themselves sender_command = received_message[received_message.find("[") + 1:received_message.find("]")] response = '' if sender_command != BOT_NAME_l: response = find_response(received_message) if len(response) > 0: slack_client.api_call("chat.postMessage", channel=on_channel, text=response + ' [' + BOT_NAME + ']', as_user=True) def parse_slack_output(slack_rtm_output): """ The Slack Real Time Messaging API is an events firehose. this parsing function returns None unless a message is directed at the Bot, based on its ID. """ output_list = slack_rtm_output if output_list and len(output_list) > 0: for output in output_list: if output and 'text' in output: return output['text'].strip().lower(), \ output['channel'] return None, None if __name__ == "__main__": READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose if slack_client.rtm_connect(): print("StarterBot connected and running!") start_message = starting_sentence + ' [' + BOT_NAME + ']' slack_client.api_call("chat.postMessage", channel='C5K82JV3Q', text=start_message, as_user=True) while True: command, channel = parse_slack_output(slack_client.rtm_read()) if command and channel: handle_command(command, channel) time.sleep(READ_WEBSOCKET_DELAY) else: print("Connection failed. Invalid Slack token or bot ID?") # endregion
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question