-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
106 lines (82 loc) · 3.71 KB
/
bot.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
from slackeventsapi import SlackEventAdapter
from slack_sdk.web import WebClient
import os, sqlite3
from flask import Flask,request,render_template
from datetime import datetime
from sentiment_analyzer import sample_analyze_sentiment
# This `app` represents your existing Flask app
app = Flask(__name__)
def create_db_tables(conn):
if conn is not None:
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS messages (
channel_id text NOT NULL,
sender_id text NOT NULL,
name text NOT NULL,
ts text NOT NULL,
sentiment integer NOT NULL
);""")
else:
print("Error! cannot create the database connection.")
return c
conn = sqlite3.connect('database.db', check_same_thread=False)
c = create_db_tables(conn)
# Initialize Slack WebClient and Event Adapter
slack_client = WebClient(os.environ["BOT_USER_ACCESS_TOKEN"])
slack_events_adapter = SlackEventAdapter(os.environ["SIGNING_SECRET"], "/slack/events", app)
# Send a message to indicate that bot is online
slack_client.chat_postMessage(channel="#general", text="Hello, I'm online now! :tada:")
# Example responder to greetings
@slack_events_adapter.on("message")
def handle_message(event_data):
# Extract message from json response
message = event_data["event"]
# Log the sended message
print("LOG: following message sent -> " + message['text'])
# Insert message into messages table
# if message['user'] is not slack_client.bots_info['bot']['id']:
print(slack_client.bots_info(token=os.environ["BOT_USER_ACCESS_TOKEN"]))
c.execute("""INSERT INTO messages(channel_id, sender_id, name, ts, sentiment)
VALUES (?,?,?,?,?);""", (message['channel'], message['user'], message['text'], message['ts'], sample_analyze_sentiment(message['text'])))
c.execute("COMMIT")
# If the incoming message contains "hi", then respond with a "Hello" message
if message.get("subtype") is None and "hi" in message.get('text'):
channel = message["channel"]
message = "Hello <@%s>! :tada:" % message["user"]
slack_client.chat_postMessage(channel=channel, text=message)
# Error events
@slack_events_adapter.on("error")
def error_handler(err):
print("ERROR: " + str(err))
@app.template_filter('ctime')
def timectime(s):
return datetime.fromtimestamp(float(s)).strftime('%a, %B %d')
# An example of one of your Flask app's routes
@app.route("/")
def hello():
members = slack_client.users_list()['members']
return render_template('mainpage.html', members = members)
@app.route("/<member_id>")
def profile(member_id=0):
# member = next(member for member in slack_client.users_list()['members'] if member['id'] == member_id)
member = {}
c.execute("""SELECT * from messages""")
records = c.fetchall()
member_records = [ record for record in records if record[1] == member_id ]
critical_records = [ record for record in member_records if record[4] < -0.7 ]
###
gevents = service.events().list(calendarId='primary', pageToken=os.environ["PAGE_TOKEN"]).execute()
events = []
for gevent in gevents[-5:]:
event = {}
event['title'] = gevent['title']
event['date'] = gevent['date']
if member['email'] in gevent['attendees']:
event['attendance'] = 'Attended'
else:
event['attendance'] = 'Absent'
events.push(event)
return render_template('profile.html', member=member, critical_records = critical_records, events=events)
# Start the server on port 3000
if __name__ == "__main__":
app.run(port=3000)