Last active
May 25, 2017 02:25
-
-
Save ketanhwr/428153d598143ef581c6a7ab9f70c383 to your computer and use it in GitHub Desktop.
A Slack bot for telling contact details of any member. Picks up the details from a Google Spreadsheet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gspread | |
from oauth2client.service_account import ServiceAccountCredentials | |
import os | |
import time | |
from slackclient import SlackClient | |
# constants | |
BOT_NAME = 'info' | |
SLACK_BOT_TOKEN = # not gonna give you that :p | |
scope = ['https://spreadsheets.google.com/feeds'] | |
credentials = ServiceAccountCredentials.from_json_keyfile_name(''' and not that too xD ''', scope) | |
READ_WEBSOCKET_DELAY = 1 | |
# instantiate Slack client | |
slack_client = SlackClient(SLACK_BOT_TOKEN) | |
# valid command | |
COMMAND = ['bot', 'info'] | |
# returns the required worksheet | |
def getSheet(): | |
gc = gspread.authorize(credentials) | |
wks = gc.open('PAG 2016-17').get_worksheet(0) | |
return wks | |
# returns all records as an associative array | |
def getRecords(): | |
sheet = getSheet() | |
return sheet.get_all_records() | |
# search all records | |
def searchRecords(records, search): | |
for record in records: | |
if search.lower() in record['NAME'].lower(): | |
return record | |
if search.lower() in record['CODEFORCES HANDLE'].lower(): | |
return record | |
if search.lower() in record['CODECHEF HANDLE'].lower(): | |
return record | |
return None | |
# return formatted string | |
def formatValue(name, value): | |
return str(name + ': ' + str(value) + ' ') | |
# format the given result for output | |
def formatResult(result): | |
response = 'No member found!' | |
if result: | |
response = '' | |
if result['NAME']: | |
response = response + formatValue('Name', result['NAME']) | |
if result['ENROLL. NO.']: | |
response = response + formatValue('Enrolment', result['ENROLL. NO.']) | |
if result['Year']: | |
response = response + formatValue('Year', result['Year']) | |
if result['Branch']: | |
response = response + formatValue('Branch', result['Branch']) | |
if result['Contact No.']: | |
response = response + formatValue('Mobile', result['Contact No.']) | |
if result['GMAIL ID']: | |
response = response + formatValue('Email', result['GMAIL ID']) | |
if result['ADDRESS']: | |
response = response + formatValue('Address', result['ADDRESS']) | |
return response | |
# handle the command and return valid info | |
def handle_command(result, channel): | |
response = result | |
slack_client.api_call('chat.postMessage', channel=channel, text=response, as_user=True) | |
# parse input message and return channel and command | |
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: | |
if output and 'text' in output: | |
words = output['text'].split() | |
if words[0].lower() == COMMAND[0] and words[1].lower() == COMMAND[1]: | |
return ' '.join(words[2:]), output['channel'] | |
return None, None | |
if __name__ == '__main__': | |
# get all records | |
records = getRecords() | |
# make a connection and handle the bot | |
if slack_client.rtm_connect(): | |
print('Info connected and running!') | |
while True: | |
search, channel = parse_slack_output(slack_client.rtm_read()) | |
if search and channel: | |
if "swegwan" in search.lower(): | |
handle_command("Our god is everywhere!", channel) | |
else: | |
record = searchRecords(records, search) | |
result = formatResult(record) | |
handle_command(result, channel) | |
time.sleep(READ_WEBSOCKET_DELAY) | |
else: | |
print('Connection failed. Invalid Slack token or bot ID?') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment