-
Notifications
You must be signed in to change notification settings - Fork 0
/
dogbot.py
47 lines (42 loc) · 1.52 KB
/
dogbot.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
import requests
import random
import os
from flask import Flask, request
app = Flask(__name__)
def get_gif():
API_KEY = os.getenv("GIPHY")
giphy_payload = {'api_key': API_KEY, 'q': 'dog', 'limit': 1, 'offset': random.randint(0, 1000), 'rating': 'g'}
giphy_request = requests.get('http://api.giphy.com/v1/gifs/search', params=giphy_payload)
giphy_response = giphy_request.json()
default_doggy = "https://media1.giphy.com/media/4Zo41lhzKt6iZ8xff9/giphy-downsized.gif?cid=5a8f66bfo4oi0rpi1ldikr8dknqdcfwlndpf7pbkvyptl1ud&rid=giphy-downsized.gif"
try:
gif_URL = giphy_response.get('data', default_doggy)[0].get('images', default_doggy).get('downsized', default_doggy).get('url', default_doggy)
except AttributeError:
gif_URL = default_doggy
return gif_URL
@app.route('/', methods=['GET', 'POST'])
def main():
return "Hello dog"
@app.route('/fetch', methods=['GET', 'POST'])
def hello_dog():
SLACK_URL = os.getenv("SLACK_URL")
data = request.form
gif_URL = get_gif()
slack_payload = {
"response_type": "in_channel",
"blocks": [
{
"type": "image",
"image_url": gif_URL,
"alt_text": "dog",
"title": {
"type": "plain_text",
"text": "arf!"
}
}
]
}
url =data['response_url']
print(url)
slack_request = requests.post(url, json=slack_payload, headers={"Content-type": 'application/json'})
return ("", 200)