Skip to content

Instantly share code, notes, and snippets.

@PandaWhoCodes
Created October 7, 2017 09:27
Show Gist options
  • Save PandaWhoCodes/8758943d1d302713766a46a2b6943456 to your computer and use it in GitHub Desktop.
Save PandaWhoCodes/8758943d1d302713766a46a2b6943456 to your computer and use it in GitHub Desktop.
from slackclient import SlackClient
import time
BOT_NAME = 'Bot Name'
# slack API
slack_client = SlackClient('xoxb-251653110707-')
# Your bot ID
BOT_ID='BOT ID'
AT_BOT = "<@" + BOT_ID + ">:"
EXAMPLE_COMMAND=''
# stats={}
from app import chat1
#sends the message to the slack bot
def send_message(text, channel):
slack_client.api_call("chat.postMessage", channel=channel,
text=text, as_user=True)
# Used for extracting the slack messages from the json structure
#can be simplified into 1-2 lines
def parse_slack_output(slack_rtm_output):
output_list = slack_rtm_output
if output_list and len(output_list) > 0:
for output in output_list:
print(output)
if output and 'text' in output and output['user']!='BOT_ID':
#making sure the bot messages are not recorded.
if AT_BOT in output['text']:
return output['text'].split(AT_BOT)[1].strip().lower(), \
output['channel']
else:
return output['text'].strip().lower(), \
output['channel']
return None, None
#rtm: Real Time Messaging
if __name__ == "__main__":
READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
if slack_client.rtm_connect():
print("Bot connected to sklack server and running!")
while True:
command, channel = parse_slack_output(slack_client.rtm_read())
if command and channel:
message = chat1(command)
send_message(message,channel)
# print(command, channel)
time.sleep(READ_WEBSOCKET_DELAY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment