Why Use a Gateway
For indie developers building Feishu (Lark) bots, the most common bottleneck isn't the Feishu SDK—it's the LLM side: registering accounts, binding payment cards, dealing with SDK differences across providers, and rewriting code every time you switch models. Last week I helped a three-person team get onboarded. The initial estimate was two days, but using a gateway approach, it took less than two hours from registration to the bot replying to its first message—and most of that time was spent waiting for Feishu's approval process. The actual coding took under twenty minutes.
The benefit of using a gateway (such as ThisToken.AI) is that it compresses "multi-model access" into a single OpenAI-compatible interface: switching models only requires changing one model name string—no reinstalling SDKs, no reworking authentication logic. For a small team where one person handles frontend, backend, and ops, what you save isn't just development time but ongoing maintenance costs too—previously, every model price change or API change meant going back to modify code; now, you barely need to touch anything.
Prerequisites
- Create a "Custom App" on the Feishu Open Platform and enable the "Bot" capability
- Configure the callback URL under "Events & Callbacks" (it's recommended to use Feishu's encryption mode—the official SDK handles the challenge verification for you)
- Register a ThisToken.AI account and create an API Key in the console (pricing is based on the official pricing page; it's pay-as-you-go, so no large upfront payments needed)
- Python 3.8+ environment, install dependencies:
pip install lark-oapi openaiCore Code
The code below is a minimal working version: when the Feishu bot receives a direct message, it calls the LLM behind the gateway and sends the reply back to the conversation.
import json
from lark_oapi.api.im.v1 import (
P2ImMessageReceiveV1, ReplyMessageRequest, ReplyMessageRequestBody
)
import lark_oapi as lark
from openai import OpenAI
# ===== 配置 =====
FEISHU_APP_ID = "你的飞书App ID"
FEISHU_APP_SECRET = "你的飞书App Secret"
THISTOKEN_API_KEY = "你的ThisToken API Key"
client = OpenAI(
api_key=THISTOKEN_API_KEY,
base_url="https://api.thistoken.ai/v1"
)
llm_client = lark.Client.builder() \
.app_id(FEISHU_APP_ID) \
.app_secret(FEISHU_APP_SECRET) \
.build()
def ask_llm(question: str) -> str:
resp = client.chat.completions.create(
model="gpt-4o-mini", # 换模型只改这一行
messages=[
{"role": "system", "content": "你是一个简洁友好的团队助手。"},
{"role": "user", "content": question},
],
)
return resp.choices[0].message.content
def do_reply(data: P2ImMessageReceiveV1) -> None:
msg = data.event.message
if msg.chat_type != "p2p": # 本示例只处理私聊
return
question = json.loads(msg.content).get("text", "")
answer = ask_llm(question)
request = ReplyMessageRequest.builder() \
.message_id(msg.message_id) \
.request_body(ReplyMessageRequestBody.builder()
.content(json.dumps({"text": answer}))
.msg_type("text")
.build()) \
.build()
llm_client.im.v1.message.reply(request)
event_handler = lark.EventDispatcherHandler.builder("", "") \
.register_p2_im_message_receive_v1(do_reply) \
.build()
ws_client = lark.ws.Client(FEISHU_APP_ID, FEISHU_APP_SECRET,
event_handler=event_handler)
if __name__ == "__main__":
ws_client.start()A few points worth noting:
- This uses Feishu's long connection mode (
lark.ws.Client), so you don't need a public server or domain—running it locally is enough to receive messages. For indie developers without ops support, this is a major time saver—it eliminates the whole process of buying a server, setting up HTTPS, and configuring authentication, which typically takes half a day. - The LLM call uses the standard OpenAI SDK with only the
base_urlchanged. If you want to compare different models later, just change themodelparameter—zero changes to the rest of the code. - For production, it's recommended to store the API Key in environment variables rather than hardcoding it into your repository.
Common Issues During Debugging
Messages received but no reply: First check the terminal logs for LLM call errors. The most common causes are an incompletely copied API Key, or the Key not having the corresponding model enabled.
Reply sent but the message is empty: Feishu requires content to be a valid JSON string—don't omit json.dumps.
Stuck on first startup: Make sure the Feishu app version has been published, the "Bot" capability is enabled, and "Receive messages" is checked under event subscriptions.
The Cost and Time Math
Here's the breakdown for a small team (excluding model usage fees—see the official pricing page for those):
| Item | Direct connection to each provider | Via gateway |
|---|---|---|
| Account registration & card binding | 30-60 min per provider | ~10 min once |
| First version of code | Write each provider's SDK separately | One set of OpenAI-compatible code |
| Switching models | Change SDK, change auth | Change one string |
| Serverless deployment | Requires public callback URL | Long connection runs locally |
After getting this setup working, the team expanded the bot's use cases from "Q&A" to "group weekly report summaries" and "on-call reminder generation." Each new feature only required changing the prompt and the event types being listened to—the main pipeline was fully reused. The first bot went from idea to launch in under two hours; starting from the second one, changes took about ten minutes each.
Next Steps
Once the direct message reply is working, you can gradually add: group chat @-mentions (handle the mentions field), conversation context (assemble history messages into messages), and streaming output (Feishu supports streaming updates for card messages). All of these build on the same gateway interface with no new integration costs.
If you haven't registered yet, you can create an account and get an API Key directly here, then run the code above: https://api.thistoken.ai/register
---
Ready to try it yourself? Sign up at https://api.thistoken.ai/register to get your API key and start building.
Ready to try Token.AI?
Create a project-level API Key, enable channels in the console, and configure routing, budgets, and audit logs.
注册 ThisToken.AI 并获取 API Key