Files
office_xbot_feishu/app/task.py
T
jianxin 57e98728eb
continuous-integration/drone/push Build is passing
add crontab task
2024-02-04 19:35:23 +08:00

104 lines
3.2 KiB
Python

import time
import json
from itertools import chain
from celery import shared_task
from celery import Task
from app.utils.utils import *
from app.utils.yd_utils import get_token, get_activity_info
@shared_task(ignore_result=False)
def add(a: int, b: int) -> int:
return a + b
@shared_task()
def block() -> None:
time.sleep(5)
@shared_task(bind=True, ignore_result=False)
def process(self: Task, total: int) -> object:
for i in range(total):
self.update_state(state="PROGRESS", meta={"current": i + 1, "total": total})
time.sleep(1)
return {"current": total, "total": total}
@shared_task(include_result=False)
def process_message_callback(
app_id, app_secret, bitable_token, table_id, cache_dir, data
) -> None:
client = lark.Client.builder().app_id(app_id).app_secret(app_secret).build()
record = {}
images = []
images_token = []
message_content = ""
# message_type = data.event.message.message_type
# content = json.loads(data.event.message.content)
# sender_id = data.event.sender.sender_id.open_id
# message_id = data.event.message.message_id
message_type = data.get("message_type")
content = data.get("content")
sender_id = data.get("sender_id")
message_id = data.get("message_id")
if message_type == "post":
content = content["content"]
for item in chain.from_iterable(content):
if item.get("tag") == "text":
message_content += item["text"]
if item.get("tag") == "img":
images.append(item["image_key"])
if message_type == "text":
message_content = content["text"].replace("@_user_1", "").strip()
record["问题概述"] = message_content
record["创建人"] = [{"id": sender_id}]
# 对 image_key 处理并并上传至多维表格
for image in images:
image_path = get_message_resource(client, message_id, image, "image", cache_dir)
print("下载文件成功:", image, image_path)
file_token = upload_media(client, image_path, "bitable_image", bitable_token)
print("上传文件成功:", file_token)
images_token.append(file_token)
if images:
record["附件"] = [{"file_token": image} for image in images_token]
record_id = add_record(client, bitable_token, table_id, record)
time.sleep(1)
update_data = {"创建人": {"id": sender_id}}
update_record(client, bitable_token, table_id, record_id, update_data)
# content = {"text": "反馈的问题已经记录到多维表格, 稍后有小伙伴跟进"}
# send_message(receive_id, content)
# 回复群组的消息
content = f'{{"text": "反馈的问题已经记录到多维表格, 稍后有小伙伴跟进"}}'
send_group_message(client, message_id, content)
@shared_task()
def sync_xbot_activity_info():
xbot_username = os.environ["XBOT_USERNAME"]
xbot_password = os.environ["XBOT_PASSWORD"]
token = get_token(xbot_username, xbot_password)
office_activity_uuids: list = get_activity_info(token)
for item in office_activity_uuids:
uuid = item.get("uuid")
package_viewName = item.get("packageViewName")
print(uuid, package_viewName)