Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #### app/botfunc.py
- def bot_process_message(command):
- response = 'I cant understand!'
- if command.startswith('hi'):
- response = 'hi friend'
- return response
- #### app/bot.py
- import os
- import time
- from slackclient import SlackClient
- import json
- from botfunc import bot_process_message
- from dotenv import load_dotenv
- load_dotenv(".env")
- BOT_ID = os.environ.get("BOT_ID")
- SLACK_BOT_TOKEN = os.environ.get("SLACK_BOT_TOKEN")
- AT_BOT = "<@" + str(BOT_ID) + ">"
- slack_client = SlackClient(SLACK_BOT_TOKEN)
- def handle_command(command, channel):
- response = bot_process_message(command)
- slack_client.api_call("chat.postMessage", channel=channel,
- text=response, as_user=True)
- def parse_slack_output(slack_rtm_output):
- print(slack_rtm_output)
- output_list = slack_rtm_output
- if output_list and len(output_list) > 0:
- for output in output_list:
- if output and 'text' in output and AT_BOT in output['text']:
- # return text after the @ mention, whitespace removed
- return output['text'].split(AT_BOT)[1].strip().lower(), \
- output['channel']
- return None, None
- def run_bot():
- if slack_client.rtm_connect():
- print("bot connected and running!")
- while True:
- command, channel = parse_slack_output(slack_client.rtm_read())
- if command and channel:
- handle_command(command, channel)
- time.sleep(1)
- else:
- print("Connection failed. Invalid Slack token or bot ID?")
- #### main.py
- from app.bot import run_bot
- if __name__ == "__main__":
- run_bot()
- ### .env
- BOT_ID = (your bot id)
- SLACK_BOT_TOKEN = (your token)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement