-
Notifications
You must be signed in to change notification settings - Fork 11
/
slack.py
49 lines (39 loc) · 1.31 KB
/
slack.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
# A Bare Bones Slack API
# Illustrates basic usage of FastAPI
from fastapi import FastAPI, HTTPException, status
from pydantic import BaseModel
from typing import List
# Message class defined in Pydantic
class Message(BaseModel):
channel: str
author: str
text: str
# Instantiate the FastAPI
app = FastAPI()
# In a real app, we would have a database.
# But, let's keep it super simple for now!
channel_list = ["general", "dev", "marketing"]
message_map = {}
for channel in channel_list:
message_map[channel] = []
@app.get("/status")
def get_status():
"""Get status of messaging server."""
return ({"status": "running"})
@app.get("/channels", response_model=List[str])
def get_channels():
"""Get all channels in list form."""
return channel_list
@app.get("/messages/{channel}", response_model=List[Message])
def get_messages(channel: str):
"""Get all messages for the specified channel."""
return message_map.get(channel)
@app.post("/post_message", status_code=status.HTTP_201_CREATED)
def post_message(message: Message):
"""Post a new message to the specified channel."""
channel = message.channel
if channel in channel_list:
message_map[channel].append(message)
return message
else:
raise HTTPException(status_code=404, detail="channel not found")