forked from rainyear/dailybot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (59 loc) · 2.06 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
from fastapi import FastAPI
import requests as _req
import time
import json
from requests.api import head
app = FastAPI()
APP_ID = "飞书 APP ID"
APP_SEC = "飞书 APP SEC"
API_PASS = " 和 GITHUB SEC 一致"
CHAT_ID = " 如何获取消息 ID"
class TToken:
def __init__(self) -> None:
self.session = _req.Session()
self.session.headers.update({
"content-type": "application/json; charset=utf-8"
})
self.Token = None
self.Expire = int(time.time())
def get_token(self):
if self.Token is None or int(time.time()) > self.Expire:
res = self.session.post(
"https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal",
json={
"app_id": APP_ID,
"app_secret": APP_SEC
})
msg = res.json()
print(msg)
if "ok" == msg.get("msg"):
self.Token = msg.get("tenant_access_token")
self.Expire = int(time.time()) + msg.get("expire")
return self.Token
ttoken = TToken()
@app.post("/feishu")
def bot(msg: dict):
return {"challenge": msg.get("challenge")}
@app.post("/feishu_send")
def send_msg(msg: dict):
if msg.get("pass") != API_PASS:
return {"msg": "Bye!"}
with open("msg_card.json", "r") as f:
msg_data = json.load(f)
msg_content = msg.get("msg")
# Msg Card Title
msg_data["elements"][0]["content"] = "**{}**".format(msg_content.get("title"))
# Msg Card Content
msg_data["elements"][2]["content"] = msg_content.get("content")
print(msg_data)
token = ttoken.get_token()
api = "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id"
res = _req.post(api, headers={
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json; charset=utf-8'
}, json={
"receive_id": CHAT_ID,
"content": json.dumps(msg_data),
"msg_type": "interactive"
})
print(res.json())