This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
114 lines (95 loc) · 3.58 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import time
from slackclient import SlackClient
import re
import random
from chatterbot import ChatBot
from datetime import datetime
import forecastio
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
logger.addHandler(ch)
# Constants
READ_WEBSOCKET_DELAY = os.environ.get("READ_WEBSOCKET_DELAY") # delay between reading from firehose
# Slack API
SLACK_BOT_ID = os.environ.get("SLACK_BOT_ID")
SLACK_BOT_TOKEN = os.environ.get("SLACK_BOT_TOKEN")
# Weather service API key and target location
FORECASTIO_API_KEY = os.getenv('FORECASTIO_API_KEY')
DARKSKY_API_KEY = os.getenv('DARKSKY_API_KEY')
LAT = os.getenv('LAT')
LNG = os.getenv('LNG')
forecast = forecastio.load_forecast(DARKSKY_API_KEY, LAT, LNG)
byHour = forecast.hourly()
logger.info('Forecast Summary: {}'.format(byHour.summary))
AT_BOT = "<@" + SLACK_BOT_ID + ">"
# Create a new instance of a ChatBot
chatbot = ChatBot(
"Ebot",
storage_adapter="chatterbot.storage.SQLStorageAdapter",
logic_adapters=[
"chatterbot.logic.MathematicalEvaluation",
'chatterbot_weather.WeatherLogicAdapter',
# "chatterbot.logic.TimeLogicAdapter",
"chatterbot.logic.BestMatch"
],
database = "database.db",
trainer = 'chatterbot.trainers.ChatterBotCorpusTrainer',
forecastio_api_key = FORECASTIO_API_KEY
)
#chatbot.train('chatterbot.corpus.english')
chatbot.train('chatterbot.corpus.custom')
# instantiate Slack & Twilio clients
slack_client = SlackClient(SLACK_BOT_TOKEN)
userList = {}
api_call = slack_client.api_call("users.list")
if api_call.get('ok'):
# retrieve all users
users = api_call.get('members')
for user in users:
userCode = '{0}'.format(user.get('id'))
try:
if user['profile']['display_name'] != '':
userList[userCode] = '%s' % user['profile']['display_name']
else:
userList[userCode] = '%s' % user['real_name']
except KeyError:
pass
def handle_command(command, channel, userID):
"""
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.
"""
attachments = None
userName = userList[userID]
response = '%s' % chatbot.get_response(command)
slack_client.api_call("chat.postMessage", channel=channel, text=response, attachments=attachments, 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 and AT_BOT in output['text']:
# return text after the @ mention, whitespace removed
return output['text'].split(AT_BOT)[1].strip().lower(), \
output['channel'], \
output['user']
return None, None, None
if __name__ == "__main__":
READ_WEBSOCKET_DELAY = 2 # 2 second delay between reading from firehose
if slack_client.rtm_connect():
print("Slack-ChatBot is connected and running!")
while True:
command, channel, userID = parse_slack_output(slack_client.rtm_read())
if command and channel:
handle_command(command, channel, userID)
time.sleep(READ_WEBSOCKET_DELAY)
else:
print("Connection failed. Invalid Slack token or bot ID?")