-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathreceiver.py
More file actions
65 lines (45 loc) · 1.67 KB
/
receiver.py
File metadata and controls
65 lines (45 loc) · 1.67 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
from json import JSONDecodeError
import aio_pika
import ujson
from aio_pika.pool import Pool
from starlette.applications import Starlette
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import PlainTextResponse
from utils.config import RABBITMQ_QUEUE
from utils.config import RABBITMQ_URL
from utils.config import VK_CONF_CODE
from utils.config import VK_SECRET_KEY
import uvicorn
app = Starlette()
conns = {}
async def get_connection():
return await aio_pika.connect_robust(RABBITMQ_URL)
async def get_channel() -> aio_pika.Channel:
async with conns["connection_pool"].acquire() as connection:
return await connection.channel()
@app.on_event("startup")
async def start():
connection_pool = Pool(get_connection, max_size=5)
conns["connection_pool"] = connection_pool
channel_pool = Pool(get_channel, max_size=10)
conns["channel_pool"] = channel_pool
@app.route("/bot-receive", methods=["POST"])
async def receiver(request: Request):
try:
json: dict = await request.json()
except JSONDecodeError:
raise HTTPException(403, "Access denied")
if json.get("secret") != VK_SECRET_KEY:
raise HTTPException(403, "Access denied")
type_event = json.get("type")
if type_event == "confirmation":
return PlainTextResponse(VK_CONF_CODE)
message: str = ujson.dumps(json)
async with conns["channel_pool"].acquire() as channel:
await channel.default_exchange.publish(
aio_pika.Message(body=message.encode()), routing_key=RABBITMQ_QUEUE
)
return PlainTextResponse("ok")
if __name__ == "__main__":
uvicorn.run(app)