-
Notifications
You must be signed in to change notification settings - Fork 1
/
troutslap.py
86 lines (68 loc) · 2.3 KB
/
troutslap.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import slack
from dotenv import load_dotenv
from flask import Flask, request
from slack.errors import SlackApiError
app = Flask(__name__)
load_dotenv()
valid = os.getenv("TOKEN")
slacktoken = os.getenv("SLACKTOKEN")
client = slack.WebClient(slacktoken)
def slack_post(channel, blocks=[], title=None, message=None, color="#999999"):
"""Send a message to Slack"""
attach = dict(fallback=message, title=title, text=message, color=color)
try:
post = client.chat_postMessage(
channel=channel,
attachments=[attach],
username="Troutslap!",
icon_emoji=":fish:",
blocks=blocks,
)
except SlackApiError:
# TODO: send something to poster here
post = None
return post
def slap_gif(channel):
"""Format a message that just sends a gif of the fish-slapping dance
from Monty Python
"""
blocks = [
{
"type": "image",
"title": {"type": "plain_text", "text": "image1", "emoji": True},
"image_url": "https://i.imgur.com/R26mope.gif",
"alt_text": "image1",
}
]
response = slack_post(channel=channel, blocks=blocks)
return response
@app.route("/", methods=["POST"])
def index():
"""Respond to Slack slash command.
Generally expects a username, and sends a bot message like:
`X slaps Y around a bit with a large trout!` where X is the sender
and Y is a username specified in the text of the slash command request
If the text is empty, it sends the fish-slapping dance gif
"""
# Verify the token sent by the slash command
if request.form["token"] != valid:
return "nope", 403
# TODO: figure out DMs
channel = request.form["channel_id"]
if request.form["text"] == "":
slap_gif(channel)
else:
slapped_user = request.form["text"]
slapping_user_id = request.form["user_id"]
slapping_user = request.form["user_name"]
message = (
f"<@{slapping_user_id}|{slapping_user}> slaps {slapped_user}"
" around a bit with a large trout!"
)
slack_post(channel=channel, message=message)
return "", 200
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5000)