From 9846c588d0e4f950aba1f07b22eaface48ddd386 Mon Sep 17 00:00:00 2001 From: lialzm <6356434+lialzm@users.noreply.github.com> Date: Mon, 3 Oct 2022 00:18:09 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=9A=E7=BB=B4?= =?UTF-8?q?=E8=A1=A8=E6=A0=BCapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- feishu/Application.py | 65 +++++++++++++------------------------------ feishu/Utils.py | 50 +++++++++++++++++++++++++++++++++ feishu/__init__.py | 2 +- 3 files changed, 71 insertions(+), 46 deletions(-) diff --git a/feishu/Application.py b/feishu/Application.py index 58646fc..082d847 100644 --- a/feishu/Application.py +++ b/feishu/Application.py @@ -15,6 +15,26 @@ from .Utils import FeishuBase, Request from .Decorator import tenant_access_token, app_access_token +class Bitable(FeishuBase): + @tenant_access_token + def add_record(self,app_token,table_id,fields): + url='/bitable/v1/apps/%s/tables/%s/records'%(app_token,table_id) + return self.request.post(url, data=fields) + + @tenant_access_token + def search_records(self,app_token,table_id,filter): + url='/bitable/v1/apps/%s/tables/%s/records?filter=%s'%(app_token,table_id,filter) + return self.request.get(url, data=None) + + @tenant_access_token + def update_record(self,app_token,table_id,record_id,fields): + url='/bitable/v1/apps/%s/tables/%s/records/%s'%(app_token,table_id,record_id) + return self.request.put(url, data=fields) + + @tenant_access_token + def tables(self,user_open_id): + url='/bitable/v1/apps/%s/tables'%(user_open_id) + return self.request.get(url, data=None) class Bot(FeishuBase): MESSAGE_MAP = { @@ -38,51 +58,6 @@ class Bot(FeishuBase): MESSAGE_BUTTON_CONFIRM = True MESSAGE_NOTICE_NAME = '通知' - def __init__(self, app_id, app_secret, retry=None): - - self.app_id = app_id - self.app_secret = app_secret - - assert app_id is None or isinstance(app_id, str), app_id - assert app_secret is None or isinstance(app_secret, str), app_secret - assert ( - retry is None - or isinstance(retry, int) - or isinstance(retry, urllib3.util.Retry) - ) - - self.request = Request( - retry=retry - ) - # assert app_verification_token is None or isinstance(app_verification_token, str), app_verification_token - - def get_tenant_access_token(self): - """ - 租户TOKEN - :return: - """ - url = "/auth/v3/tenant_access_token/internal/" - req_body = { - "app_id": self.app_id, - "app_secret": self.app_secret - } - self.request.tenant_access_token = self.request.get(url, req_body) - self.request.tenant_access_token['expire'] += time.time() - return self.request.tenant_access_token - - def get_app_access_token(self): - """ - APP TOKEN - :return: - """ - url = "/auth/v3/app_access_token/internal/" - req_body = { - "app_id": self.app_id, - "app_secret": self.app_secret - } - self.request.app_access_token = self.request.get(url, req_body) - self.request.app_access_token['expire'] += time.time() - return self.request.app_access_token @tenant_access_token def send_user_message(self, user_open_id, text=None): diff --git a/feishu/Utils.py b/feishu/Utils.py index 8e016f8..9cebb7c 100644 --- a/feishu/Utils.py +++ b/feishu/Utils.py @@ -9,6 +9,7 @@ import sys import json import requests +import time from .Logs import logger from .FeishuException import RequestException @@ -16,6 +17,52 @@ class FeishuBase: retry = 3 + def __init__(self, app_id, app_secret, retry=None): + + self.app_id = app_id + self.app_secret = app_secret + + assert app_id is None or isinstance(app_id, str), app_id + assert app_secret is None or isinstance(app_secret, str), app_secret + assert ( + retry is None + or isinstance(retry, int) + or isinstance(retry, urllib3.util.Retry) + ) + + self.request = Request( + retry=retry + ) + # assert app_verification_token is None or isinstance(app_verification_token, str), app_verification_token + + def get_tenant_access_token(self): + """ + 租户TOKEN + :return: + """ + url = "/auth/v3/tenant_access_token/internal/" + req_body = { + "app_id": self.app_id, + "app_secret": self.app_secret + } + self.request.tenant_access_token = self.request.get(url, req_body) + self.request.tenant_access_token['expire'] += time.time() + return self.request.tenant_access_token + + def get_app_access_token(self): + """ + APP TOKEN + :return: + """ + url = "/auth/v3/app_access_token/internal/" + req_body = { + "app_id": self.app_id, + "app_secret": self.app_secret + } + self.request.app_access_token = self.request.get(url, req_body) + self.request.app_access_token['expire'] += time.time() + return self.request.app_access_token + class Request(FeishuBase): @@ -43,6 +90,9 @@ def __init__(self, timeout=None, retry=None, **kwargs): def get(self, url, data): return self.response(url, data, 'get') + def put(self, url, data): + return self.response(url, data, 'put') + def post(self, url, data=None): return self.response(url, data, 'post') diff --git a/feishu/__init__.py b/feishu/__init__.py index 37f1b08..53d3466 100644 --- a/feishu/__init__.py +++ b/feishu/__init__.py @@ -10,7 +10,7 @@ __all__ = ('set_log_level', 'Bot') from .Logs import set_log_level -from .Application import Bot +from .Application import Bot,Bitable From e4afb1d64a511adecf7766b4d2e8fca4ea96fedf Mon Sep 17 00:00:00 2001 From: lialzm <6356434+lialzm@users.noreply.github.com> Date: Tue, 21 Feb 2023 16:42:49 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=91=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- feishu/Application.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/feishu/Application.py b/feishu/Application.py index 082d847..ff5ff70 100644 --- a/feishu/Application.py +++ b/feishu/Application.py @@ -62,9 +62,9 @@ class Bot(FeishuBase): @tenant_access_token def send_user_message(self, user_open_id, text=None): assert all([text]), 'At least one of "text" or "data" is not empty' - url = "/message/v4/send/" + url = "/im/v1/messages?receive_id_type=open_id" data = { - "open_id": user_open_id, + "receive_id": user_open_id, "msg_type": "text", "content": { "text": text From 07cb6ae4361430fcb481af0ede5f473ce00ca633 Mon Sep 17 00:00:00 2001 From: lialzm <6356434+lialzm@users.noreply.github.com> Date: Tue, 21 Feb 2023 17:13:45 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- feishu/Application.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/feishu/Application.py b/feishu/Application.py index ff5ff70..712732c 100644 --- a/feishu/Application.py +++ b/feishu/Application.py @@ -8,6 +8,7 @@ """ import time import urllib3 +import json from datetime import datetime @@ -63,12 +64,13 @@ class Bot(FeishuBase): def send_user_message(self, user_open_id, text=None): assert all([text]), 'At least one of "text" or "data" is not empty' url = "/im/v1/messages?receive_id_type=open_id" + content={ + "text": text + } data = { "receive_id": user_open_id, "msg_type": "text", - "content": { - "text": text - } + "content":json.dumps(content) } return self.request.post(url, data=data)