How to Send Zabbix Alerts to Feishu Groups Using Python API
This guide explains how to integrate Zabbix alert notifications with Feishu by obtaining necessary tokens, retrieving user and chat IDs, and using Python code to send text messages to Feishu groups or users, including configuration steps within Zabbix.
Implementation Overview
Feishu provides rich APIs for sending messages. This guide shows how to use the Feishu API to send text messages from Zabbix alerts.
Authorization Tokens
Three tokens are required:
app_access_token – access App resources.
tenant_access_token – access enterprise resources.
user_access_token – access user resources.
Getting Authorization Tokens
1. Obtain App ID and App Secret
Log in to the developer console, create a self‑built enterprise app, and retrieve the App ID and App Secret.
2. Get tenant_access_token
Use the self‑built app method to request a tenant access token.
3. Request token with App ID and Secret
def gettenant_access_token():
tokenurl="https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
headers={"Content-Type":"application/json"}
data={"app_id":"cli_9ec625abcdefg","app_secret":"f716Gi27Yi25n5K0Wbafgwghhstv"}
request=requests.post(url=tokenurl,headers=headers,json=data)
response=json.loads(request.content)['tenant_access_token']
return responseGetting user_id
user_id can be retrieved by mobile number or email via the Feishu user API.
def getuserid(tenant_access_token):
# mobiles="15101234584"
userurl="https://open.feishu.cn/open-apis/user/v1/batch_get_id?mobiles=%s"%mobiles
headers={"Authorization":"Bearer %s"%tenant_access_token}
request=requests.get(url=userurl,headers=headers)
response=json.loads(request.content)['data']['mobile_users'][mobiles][0]['user_id']
return responseGetting chat_id
Two methods exist: add the bot to a group and read the group info, or create a group via the bot. The example uses the first method.
Add bot to group
Request chat_id
def getchatid(tenant_access_token):
#获取chatid
chaturl="https://open.feishu.cn/open-apis/chat/v4/list?page_size=20"
headers={"Authorization":"Bearer %s"%tenant_access_token,"Content-Type":"application/json"}
request=requests.get(url=chaturl,headers=headers)
response=json.loads(request.content)['data']['groups'][0]['chat_id']
return responseSend Message to Feishu Group or User
Provide user_id, chat_id, and tenant_access_token, then send the alert text.
def sendmes(user_id,chat_id,tenant_access_token):
#向群里发送消息
sendurl="https://open.feishu.cn/open-apis/message/v4/send/"
headers={"Authorization":"Bearer %s"%tenant_access_token,"Content-Type":"application/json"}
data={"chat_id":chat_id,
"msg_type":"text",
"content":{
"text":"%s<at user_id=\"%s\">test</at>"%(messages,user_id)
}
}
request=requests.post(url=sendurl,headers=headers,json=data)
print(request.content)Configure Zabbix Alert Action
Media Type
Set the correct parameter order.
User Contact
Enter the phone number registered in Feishu.
Action
Alert Test
Disable one Windows agent to trigger a test alert.
Full code is available on GitHub: https://github.com/sunsharing-note/zabbix/blob/master/feishu.py
Feel free to discuss.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
