Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import time
- from slackclient import SlackClient
- from github import Github
- GITHUB_ID = os.environ.get('GITHUB_ID')
- GITHUB_PASS = os.environ.get('GITHUB_PASS')
- github = Github(GITHUB_ID, GITHUB_PASS)
- BOT_ID = os.environ.get("BOT_ID")
- AT_BOT = "<@" + BOT_ID + ">"
- EXAMPLE_COMMAND = "do"
- BOT_NAME = 'suki-bot'
- slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
- org = github.get_organization(os.environ.get('GITHUB_ORG'))
- repo = org.get_repo(os.environ.get('GITHUB_REPO'))
- def do_execute(parts):
- if len(parts) < 2:
- return "Not Execute."
- response = "Not Execute."
- if parts[1] == 'issues':
- response = ""
- issues = repo.get_issues()
- if issues:
- for issue in issues:
- response += "[%04d]"%(issue.number) + issue.title + "\n"
- return response
- def handle_command(command, 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.
- """
- print(command, channel)
- response = "Not sure what you mean. Use the *" + EXAMPLE_COMMAND + \
- "* command with numbers, delimited by spaces."
- parts = command.strip().split()
- response = do_execute(parts)
- try:
- slack_client.api_call("chat.postMessage", channel=channel,
- text=response, as_user=True)
- except Exception:
- print("[error] : " + response)
- 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 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
- if __name__ == "__main__":
- READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
- if slack_client.rtm_connect():
- print("%s connected and running!"%(BOT_NAME))
- 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?")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement