---- 凭证统一从环境变量读取,禁止写进代码 ----
APP_ID = os.environ["FEISHU_APP_ID"]
APP_SECRET = os.environ["FEISHU_APP_SECRET"]
GATEWAY_KEY = os.environ["THISTOKEN_API_KEY"]
---- 网关层:所有模型请求走统一出口 ----
llm = OpenAI(
api_key=GATEWAY_KEY,
base_url="https://api.thistoken.ai/v1",
)
def ask_model(question: str) -> str:
resp = llm.chat.completions.create(
model="gpt-4o-mini", # 模型名按网关文档选择
messages=[
{"role": "system", "content": "你是团队助手,回答保持简洁。"},
{"role": "user", "content": question},
],
)
return resp.choices[0].message.content
def on_message(data: P2ImMessageReceiveV1) -> None:
msg = data.event.message
if msg.message_type != "text":
return
question = json.loads(msg.content)["text"]
answer = ask_model(question)
request = ReplyMessageRequest.builder() \
.message_id(msg.message_id) \
.request_body(ReplyMessageRequestBody.builder()
.content(json.dumps({"text": answer}))
.msg_type("text")
.build()) \
.build()
client.im.v1.message.reply(request)
event_client = lark.ws.Client(APP_ID, APP_SECRET,
event_handler=lark.EventDispatcherHandler
.builder("", "")
.register_p2_im_message_receive_v1(on_message)
.build())
client = lark.Client.builder().app_id(APP_ID).app_secret(APP_SECRET).build()
if __name__ == "__main__":
event_client.start()
After installing the dependencies, you can run it:
pip install lark-oapi openai
export FEISHU_APP_ID=xxx FEISHU_APP_SECRET=xxx THISTOKEN_API_KEY=xxx
python bot.py
@-mention the bot in a group and ask a question; if you get a reply within a few seconds, the pipeline is working.
## Step 4: Three Checkpoints for Managers
**Pre-launch review**. Confirm three things: whether all credentials go through environment variables or secret management; whether the bot's permissions are minimized; whether the replies might leak internal information—it's recommended to add scope restrictions to the system prompt and run it in a test group for one to two weeks first.
**Cost and usage monitoring**. Since all requests go through the same gateway Key, usage data is naturally centralized. Check call volume and cost distribution once a week (refer to the official pricing page for specific rates), and promptly investigate which scenario is burning money if you notice abnormal fluctuations.
**Degradation and handover plans**. At minimum, document clearly: who to contact if a Key is leaked, and how to revoke and reissue it immediately; which fallback model to switch to if one becomes unavailable (the beauty of the gateway is that switching models only requires changing one `model` parameter); who the backup is when the person in charge is on vacation.
## Final Words
The pitfall small teams fall into most easily isn't failing to write the code—it's tying the system to personal accounts and personal memory. By consolidating the bot's model access into a single team gateway, credentials, costs, and risks all have a handle to grab onto. The whole setup takes no more than an afternoon, and what it saves is the communication cost of every future staffing change and model adjustment.
If you don't yet have a team-level gateway account, you can start registering here: https://api.thistoken.ai/register
---
Every example in this post runs with a single API key — get yours at https://api.thistoken.ai/register and start in minutes.Хотите попробовать Token.AI?
Создайте API Key уровня проекта, включите каналы в консоли и настройте маршрутизацию, бюджеты и журналы аудита.
注册 ThisToken.AI 并获取 API Key