Compare commits
2 Commits
33631e2b94
...
c8d5195669
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c8d5195669 | ||
|
|
ea42cef954 |
104
recipe/models.py
104
recipe/models.py
@ -5,8 +5,6 @@ import datetime
|
||||
from utils import const
|
||||
import utils
|
||||
|
||||
|
||||
|
||||
class Recipe(models.Model):
|
||||
name = models.CharField(max_length=128)
|
||||
recipe_type = models.CharField(max_length=32,
|
||||
@ -30,6 +28,108 @@ class Recipe(models.Model):
|
||||
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def create_from_str(cls, content):
|
||||
content = content.strip()
|
||||
if not content:
|
||||
return
|
||||
name, *data = content.split(' ')
|
||||
recipe_type = rate = difficulty = None
|
||||
keys = [recipe_type, rate, difficulty]
|
||||
for x in range(len(data)):
|
||||
keys[x] = data[x]
|
||||
recipe_type, rate, difficulty = keys
|
||||
|
||||
if recipe_type == '肉':
|
||||
recipe_type = const.RECIPE_TYPE_MEAT
|
||||
elif recipe_type == '菜':
|
||||
recipe_type = const.RECIPE_TYPE_VEGETABLE
|
||||
elif recipe_type == '汤':
|
||||
recipe_type = const.RECIPE_TYPE_SOUP
|
||||
else:
|
||||
recipe_type = const.RECIPE_TYPE_MEAT
|
||||
if rate:
|
||||
try:
|
||||
rate = int(rate)
|
||||
except:
|
||||
rate = 0
|
||||
else:
|
||||
rate = 0
|
||||
if difficulty:
|
||||
try:
|
||||
difficulty = int(difficulty)
|
||||
except:
|
||||
difficulty = 0
|
||||
else:
|
||||
difficulty = 0
|
||||
recipe = cls.objects.create(
|
||||
name=name,
|
||||
recipe_type=recipe_type,
|
||||
rate=rate,
|
||||
difficulty=difficulty
|
||||
)
|
||||
return recipe
|
||||
|
||||
@property
|
||||
def display_recipe_type(self):
|
||||
if self.recipe_type == const.RECIPE_TYPE_VEGETABLE:
|
||||
return '菜'
|
||||
elif self.recipe_type == const.RECIPE_TYPE_MEAT:
|
||||
return '肉'
|
||||
elif self.recipe_type == const.RECIPE_TYPE_SOUP:
|
||||
return '汤'
|
||||
return '肉'
|
||||
|
||||
@property
|
||||
def display_rate(self):
|
||||
return '🍚' * self.rate
|
||||
|
||||
@property
|
||||
def display_difficult(self):
|
||||
return '⭐️' * self.difficulty
|
||||
|
||||
def construct_lart_card(self):
|
||||
|
||||
data = {
|
||||
"config": {
|
||||
"wide_screen_mode": True
|
||||
},
|
||||
"elements": [
|
||||
{
|
||||
"tag": "markdown",
|
||||
"content": "# **%s**\n类型:%s\n难度:%s\n评分:%s" % (self.name, self.display_recipe_type, self.display_difficult, self.display_rate)
|
||||
},
|
||||
{
|
||||
"tag": "action",
|
||||
"actions": [
|
||||
{
|
||||
"tag": "button",
|
||||
"text": {
|
||||
"tag": "plain_text",
|
||||
"content": "查看"
|
||||
},
|
||||
"multi_url": {
|
||||
"url": "https://recipe.tunpok.com/recipe/%s" % self.id,
|
||||
"android_url": "https://recipe.tunpok.com/recipe-mobile/recipe/%s" % self.id,
|
||||
"ios_url": "https://recipe.tunpok.com/recipe-mobile/recipe/%s" % self.id,
|
||||
"pc_url": "https://recipe.tunpok.com/recipe/%s" % self.id
|
||||
},
|
||||
"type": "primary"
|
||||
},
|
||||
{
|
||||
"tag": "button",
|
||||
"text": {
|
||||
"tag": "plain_text",
|
||||
"content": "删除"
|
||||
},
|
||||
"type": "danger"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
return data
|
||||
|
||||
|
||||
class DailyRecipe(models.Model):
|
||||
recipes = models.ManyToManyField(Recipe)
|
||||
|
||||
@ -15,6 +15,9 @@ import time
|
||||
import subprocess
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
import recipe.models
|
||||
from utils import const
|
||||
|
||||
APP_VERIFICATION_TOKEN = 'uKQQiOVMYg2cTgrjkyBmodrHTUaCXzG3'
|
||||
APP_ID = 'cli_a115fe8b83f9100c'
|
||||
APP_SECRET = 'yuSQenId0VfvwdZ3qL9wMd8FpCMEUL0u'
|
||||
@ -191,7 +194,9 @@ class RequestHandler(BaseHTTPRequestHandler):
|
||||
if orig_text.startswith('/'):
|
||||
redis_cli.set(event_id, int(time.time()), ex=60*60*7)
|
||||
if orig_text not in ['/last', '/del']:
|
||||
if not orig_text.startswith('/deploy '):
|
||||
for action_ in ['/deploy ', '/菜谱 ']:
|
||||
if orig_text.startswith(action_):
|
||||
break
|
||||
self.msg_compoment(access_token, open_id, '指令错误')
|
||||
return
|
||||
if orig_text == '/last':
|
||||
@ -221,6 +226,16 @@ class RequestHandler(BaseHTTPRequestHandler):
|
||||
else:
|
||||
self.msg_compoment(access_token, open_id, '⚠️ %s 不存在 ⚠️' % site_)
|
||||
return
|
||||
elif orig_text.startswith('/菜谱 '):
|
||||
content = orig_text.split('/菜谱 ')[1]
|
||||
recipe_ = recipe.models.Recipe.create_from_str(content)
|
||||
if recipe_:
|
||||
self.msg_compoment(access_token, open_id, None,
|
||||
const.LARK_WEBHOOK_MSG_TYPE_INTERACTIVE,
|
||||
recipe_.construct_lart_card())
|
||||
else:
|
||||
self.msg_compoment(access_token, open_id, '⚠️ 创建失败 ⚠️')
|
||||
return
|
||||
|
||||
try:
|
||||
toot_resp = mastodon_cli.status_post(text)
|
||||
@ -245,18 +260,22 @@ class RequestHandler(BaseHTTPRequestHandler):
|
||||
self.end_headers()
|
||||
self.wfile.write(body.encode())
|
||||
|
||||
def send_message(self, token, open_id, text):
|
||||
def send_message(self, token, open_id, text, msg_type=None, content=None):
|
||||
url = "https://open.feishu.cn/open-apis/message/v4/send/"
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer " + token
|
||||
}
|
||||
if not msg_type:
|
||||
msg_type = const.LARK_WEBHOOK_MSG_TYPE_TEXT
|
||||
req_body = {
|
||||
"msg_type": "text",
|
||||
"content": {
|
||||
"text": text
|
||||
}
|
||||
"msg_type": msg_type
|
||||
}
|
||||
if msg_type == const.LARK_WEBHOOK_MSG_TYPE_TEXT:
|
||||
req_body['content'] = {'text': text}
|
||||
elif msg_type == const.LARK_WEBHOOK_MSG_TYPE_INTERACTIVE:
|
||||
req_body['card'] = content
|
||||
|
||||
req_body = dict(req_body, **open_id) # 根据open_id判断返回域
|
||||
|
||||
data = bytes(json.dumps(req_body), encoding='utf8')
|
||||
@ -275,8 +294,8 @@ class RequestHandler(BaseHTTPRequestHandler):
|
||||
code,
|
||||
rsp_dict.get("msg", ""))
|
||||
|
||||
def msg_compoment(self, token, open_id, text):
|
||||
self.send_message(token, open_id, text)
|
||||
def msg_compoment(self, token, open_id, text, msg_type=None, content=None):
|
||||
self.send_message(token, open_id, text, msg_type, content)
|
||||
|
||||
def run():
|
||||
port = 5000
|
||||
|
||||
@ -22,3 +22,4 @@ FILTER_GTE_LTE = ['exact', 'gte', 'gt', 'lte', 'lt']
|
||||
|
||||
|
||||
LARK_WEBHOOK_MSG_TYPE_TEXT = 'text'
|
||||
LARK_WEBHOOK_MSG_TYPE_INTERACTIVE = 'interactive'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user