-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.py
More file actions
179 lines (129 loc) · 4.6 KB
/
demo.py
File metadata and controls
179 lines (129 loc) · 4.6 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import requests
import os
import time
import json
import flask
import resources
import functools
from pprint import pprint as pp
from flask import Flask, make_response
from chat_bot import ChatBot
# ----------------------------------------------------------------------------------------------------------------
SLACK_BOT_TOKEN = os.environ.get('SLACK_BOT_TOKEN')
SLACK_WEBHOOK_INC = os.environ.get('SLACK_WEBHOOK_INC')
SLACK_VERIFICATION_TOKEN = os.environ.get('SLACK_VERIFICATION_TOKEN')
IMGUR_CLIENT_ID = os.environ.get('IMGUR_CLIENT_ID')
IMGUR_CLIENT_SECRET = os.environ.get('IMGUR_CLIENT_SECRET')
# ----------------------------------------------------------------------------------------------------------------
app = Flask(__name__)
# ----------------------------------------------------------------------------------------------------------------
def demo_slack_test():
response = requests.post("https://slack.com/api/api.test")
pp("response from slack [%d]: %s" % (
response.status_code,
response.text
))
# -----------------------------------------------------------------------------
def slack_send_webhook(text, channel, **kwargs):
data = {
"channel": channel,
"text": text
}
data.update(kwargs)
response = requests.post(
url=SLACK_WEBHOOK_INC,
data=json.dumps(data),
headers={'content-type': 'application/json'}
)
pp("response from 'send_webhook' [%d]: %s" % (
response.status_code,
response.text
))
# -----------------------------------------------------------------------------
def slack_send_response(text, url, **kwargs):
data = {
"response_type": "in_channel",
"text": text
}
data.update(kwargs)
response = requests.post(
url=url,
data=json.dumps(data),
headers={'content-type': 'application/json'}
)
pp("response from 'slack_send_response' [%d]: %s" % (
response.status_code,
response.text
))
#------------------------------------------------------------------------------
@app.route('/api/v1/btc', methods=['POST'])
def on_btc():
response = requests.get(
url='https://api.coindesk.com/v1/bpi/currentprice.json',
)
resp_obj = json.loads(response.text)
pp(resp_obj)
request = flask.request.values
slack_send_response(
text=':us:: %s' % resp_obj['bpi']['USD']['rate'],
url=request['response_url'],
icon=':chart_with_upwards_trend:'
)
# return make_response(':us:: %s' % resp_obj['bpi']['USD']['rate'], 200)
return make_response('', 200)
# ----------------------------------------------------------------------------------------------------------------
def slack_post_msg(text, channel, **kwargs):
data = {
"token": SLACK_BOT_TOKEN,
"channel": channel,
"text": text
}
data.update(kwargs)
response = requests.post(
url="https://slack.com/api/chat.postMessage",
data=data
)
pp("response from 'slack_post_msg' [%d]: %s" % (
response.status_code,
json.dumps(json.loads(response.text), indent=4)
))
#------------------------------------------------------------------------------
def debug_request(request):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
print('-------------------')
print('%s:' % func.__name__)
print('-------------------')
pp(eval(request), indent=8)
print('\n\n')
return func(*args, **kw)
return wrapper
return decorator
# ----------------------------------------------------------------------------------------------------------------
def demo_slack_incoming_msg_handler():
app.run(host='0.0.0.0', port=8080, threaded=True)
# ----------------------------------------------------------------------------------------------------------------
def main():
# demo_slack_test()
# slack_send_webhook(
# text='Hello from epic bot!',
# #channel='#general',
# channel='#random', #"#random"
# icon_emoji=':sunglasses:'
# )
# slack_post_msg(
# text='Hello from epic bot!',
# channel='#general',
# #channel='#random',
# icon_emoji=':sunglasses:'
# )
demo_slack_incoming_msg_handler()
# chat_bot = ChatBot(
# slack_bot_token=SLACK_BOT_TOKEN,
# read_websocket_delay_sec=0.3
# )
# chat_bot.run()
# ----------------------------------------------------------------------------------------------------------------
if __name__ == '__main__':
main()