-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
68 lines (58 loc) · 2.19 KB
/
main.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
import os
from fastapi import FastAPI, Request
from dotenv import load_dotenv
import urllib.request
from urllib.parse import quote
from pathlib import Path
import brain
app = FastAPI()
# Save your config file as "telegram-bot-config.env" for recieving updates on Telegram
try:
dotenv_path = Path('telegram-bot-config.env')
load_dotenv(dotenv_path=dotenv_path)
botToken = os.getenv('BOT_TOKEN')
ownerId = os.getenv('OWNER_ID')
except:
None
@app.post("/move")
async def handle_move(request: Request):
data = await request.json()
move = brain.generate_smart_move(data, "survival")
return {"move": move}
@app.post("/start")
async def handle_start(request: Request):
data = await request.json()
print(f"Battle started!\n{data['game']}")
try:
snakesMessage = '\n<b>Snake details :</b>\n'
for i in data['board']['snakes']:
snakesMessage += f" - <code>{i['name']}</code>\n"
message = f"<b>Battle started!</b>\n\n<b>Game ID :</b> <code>{data['game']['id']}</code>\n<b>Board :</b> <code>{data['board']['width']}x{data['board']['height']}</code>\n{snakesMessage}\n<a href='https://play.battlesnake.com/g/{data['game']['id']}'>View Match</a>"
urllib.request.urlopen(
f"https://api.telegram.org/bot{botToken}/sendMessage?text={quote(message)}&chat_id={ownerId}&parse_mode=HTML&disable_web_page_preview=True")
except:
None
return "ok"
@app.get("/")
def handle_info():
print("INFO Battlesnake information captured")
return {
"apiversion": "1",
"author": "dvishal485",
"color": "#3E338F",
"head": "evil",
"tail": "flame",
}
@app.post("/end")
async def end(request: Request):
data = await request.json()
print(f"Battle ended!")
print(data)
try:
message = f"<b>Match ended!</b>\n<b>Game ID:</b> <code>{data['game']['id']}</code>"
message += f"\n\n<a href='https://play.battlesnake.com/g/{data['game']['id']}'>View Match</a>"
urllib.request.urlopen(
f"https://api.telegram.org/bot{botToken}/sendMessage?text={quote(message)}&chat_id={ownerId}&parse_mode=HTML&disable_web_page_preview=True")
except:
None
return "ok"