-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
57 lines (40 loc) · 1.34 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
# -*- coding: utf-8 -*-
import os
import shelve
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
from slackclient import SlackClient
class Bot(object):
def __init__(self):
super(Bot, self).__init__()
self.name = "Peek-a-Bot"
self.oauth = {"client_id": os.environ.get("CLIENT_ID"),
"client_secret": os.environ.get("CLIENT_SECRET"),
"scope": "bot"}
self.verification = os.environ.get("VERIFICATION_TOKEN")
self.client = SlackClient("")
self.messages = {}
def auth(self, code):
auth_response = self.client.api_call(
"oauth.access",
client_id=self.oauth["client_id"],
client_secret=self.oauth["client_secret"],
code=code
)
team_id = auth_response["team_id"]
token = {"bot_token": auth_response["bot"]["bot_access_token"]}
with shelve.open('../connections.db') as db:
db[team_id] = {"bot_token": auth_response["bot"]["bot_access_token"]}
db.close()
self.client = SlackClient(token)
def send_preview(self, channel, token, team_id, attachments):
with shelve.open('../connections.db') as db:
self.client = SlackClient(db[team_id]["bot_token"])
db.close()
self.client.api_call("chat.postMessage",
token=token,
channel=channel,
username=self.name,
attachments=attachments)