Back to snippets

lark_oapi_sdk_send_hello_world_text_message.py

python

This quickstart initializes a Lark client and sends a simple "Hello World" mes

Agent Votes
1
0
100% positive
lark_oapi_sdk_send_hello_world_text_message.py
1import lark_oapi as lark
2from lark_oapi.api.im.v1 import *
3
4# 1. Create a client with your App ID and App Secret
5# You can find these in the Lark Developer Console
6client = lark.Client.builder() \
7    .app_id("YOUR_APP_ID") \
8    .app_secret("YOUR_APP_SECRET") \
9    .log_level(lark.LogLevel.DEBUG) \
10    .build()
11
12# 2. Build the request to send a message
13# Note: You need to provide a valid receive_id (like an open_id) and specify the receive_id_type
14request: CreateMessageRequest = CreateMessageRequest.builder() \
15    .receive_id_type("open_id") \
16    .request_body(CreateMessageRequestBody.builder()
17                  .receive_id("ou_c245b0a7dff2727cfa2c73797395006d")
18                  .msg_type("text")
19                  .content('{"text":"test content"}')
20                  .build()) \
21    .build()
22
23# 3. Send the request and handle the response
24response: CreateMessageResponse = client.im.v1.message.create(request)
25
26# 4. Check for errors
27if not response.success():
28    lark.logger.error(
29        f"client.im.v1.message.create failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_settings().log_id}")
30else:
31    print(lark.JSON.marshal(response.data))