Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·318 lines (269 loc) · 11.5 KB
/
Copy pathcli.py
File metadata and controls
executable file
·318 lines (269 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python3
"""
Feishu Bridge CLI — bot messaging entry point.
Provides send-message, send-image, send-audio for bot-initiated messages.
For documents, spreadsheets, wiki, calendar, tasks, mail, etc.,
use the official Lark CLI: ~/.local/bin/lark <command>
Usage:
feishu-cli send-message --chat-id oc_xxx --text "Hello"
feishu-cli send-image --chat-id oc_xxx --file photo.png
feishu-cli send-audio --chat-id oc_xxx --file voice.opus
feishu-cli prompt [--summary]
"""
import argparse
import json
import os
import sys
from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
def _load_config():
"""Load app credentials from bridge config."""
_env_file = Path.home() / ".config" / "feishu-bridge" / ".env"
if _env_file.is_file():
with open(_env_file) as _f:
for _line in _f:
_line = _line.strip()
if _line and not _line.startswith("#") and "=" in _line:
_k, _, _v = _line.partition("=")
os.environ.setdefault(_k.strip(), _v.strip())
bot_name = os.environ.get("FEISHU_BOT_NAME")
from feishu_bridge.config import resolve_config_path
try:
config_path = resolve_config_path()
except SystemExit:
print(json.dumps({"error": "No config file found. Set $FEISHU_BRIDGE_CONFIG or create ~/.config/feishu-bridge/config.json"}))
sys.exit(1)
try:
with open(config_path) as f:
config = json.load(f)
except (OSError, json.JSONDecodeError) as e:
print(json.dumps({"error": f"Failed to read config: {e}"}))
sys.exit(1)
for bot in config.get("bots", []):
if bot_name and bot.get("name") != bot_name:
continue
app_id = os.path.expandvars(bot.get("app_id", bot.get("feishu_app_id", "")))
app_secret = os.path.expandvars(bot.get("app_secret", bot.get("feishu_app_secret", "")))
if app_id and app_secret:
return {"app_id": app_id, "app_secret": app_secret}
if config.get("bots"):
bot = config["bots"][0]
app_id = os.path.expandvars(bot.get("app_id", bot.get("feishu_app_id", "")))
app_secret = os.path.expandvars(bot.get("app_secret", bot.get("feishu_app_secret", "")))
if app_id and app_secret:
return {"app_id": app_id, "app_secret": app_secret}
print(json.dumps({"error": "No bot config found"}))
sys.exit(1)
def _build_lark_client(config=None):
"""Build a lark_oapi Client from config."""
import lark_oapi as lark
if config is None:
config = _load_config()
return config, lark.Client.builder() \
.app_id(config["app_id"]) \
.app_secret(config["app_secret"]) \
.domain(lark.FEISHU_DOMAIN) \
.log_level(lark.LogLevel.WARNING) \
.build()
def _output(result):
"""Print result as JSON."""
if result is None:
print(json.dumps({"error": "Auth failed — authorization card sent"}))
sys.exit(1)
print(json.dumps(result, ensure_ascii=False, default=str))
def main():
parser = argparse.ArgumentParser(
description="Feishu Bridge CLI — bot messaging",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
sub = parser.add_subparsers(dest="command", required=True)
# --- IM (bot messages) ---
p = sub.add_parser("send-message",
help="Send a bot message to a chat (no user auth needed)")
p.add_argument("--chat-id", required=True,
help="Feishu chat_id (e.g. oc_xxx)")
p.add_argument("--text", help="Plain text message content")
p.add_argument("--msg-type", default="text",
help="Message type: text, interactive, post (default: text)")
p.add_argument("--content",
help="Raw JSON content string (for non-text msg types)")
p = sub.add_parser("prompt",
help="Output LLM system prompt for feishu-cli usage")
p.add_argument("--summary", action="store_true",
help="Output short summary instead of full reference")
p = sub.add_parser("send-audio",
help="Upload audio file and send as audio message")
p.add_argument("--chat-id", required=True,
help="Feishu chat_id (e.g. oc_xxx)")
p.add_argument("--file", required=True,
help="Path to audio file (opus preferred, wav accepted)")
p.add_argument("--duration", type=int,
help="Audio duration in milliseconds (auto-detected if omitted)")
p = sub.add_parser("send-image",
help="Upload image file and send as image message")
p.add_argument("--chat-id", required=True,
help="Feishu chat_id (e.g. oc_xxx)")
p.add_argument("--file", required=True,
help="Path to image file (png, jpg, etc.)")
args = parser.parse_args()
# --- No-auth commands ---
if args.command == "prompt":
filename = "cli_prompt_summary.md" if args.summary else "cli_prompt.md"
prompt_path = SCRIPT_DIR / "data" / filename
if not prompt_path.exists():
print(f"Error: {filename} not found", file=sys.stderr)
sys.exit(1)
text = prompt_path.read_text()
cli_abs = os.path.abspath(sys.argv[0])
text = text.replace("feishu-cli", cli_abs)
print(text, end="")
return
# --- Bot-only commands (no user auth needed) ---
if args.command == "send-message":
if args.text and args.content:
_output({"error": "--text and --content are mutually exclusive"})
sys.exit(1)
if args.text and args.msg_type != "text":
_output({"error": "--text can only be used with --msg-type text"})
sys.exit(1)
try:
from lark_oapi.api.im.v1 import (
CreateMessageRequest, CreateMessageRequestBody,
)
config, client = _build_lark_client()
if args.text:
content = json.dumps({"text": args.text})
elif args.content:
content = args.content
else:
_output({"error": "Either --text or --content is required"})
sys.exit(1)
body = CreateMessageRequestBody.builder() \
.receive_id(args.chat_id) \
.msg_type(args.msg_type) \
.content(content) \
.build()
req = CreateMessageRequest.builder() \
.receive_id_type("chat_id") \
.request_body(body) \
.build()
resp = client.im.v1.message.create(req)
if resp.success():
mid = resp.data.message_id if resp.data else None
_output({"message_id": mid})
else:
_output({"error": resp.msg, "code": resp.code})
sys.exit(1)
except SystemExit:
raise
except Exception as e:
_output({"error": str(e)})
sys.exit(1)
return
if args.command == "send-audio":
try:
from lark_oapi.api.im.v1 import (
CreateFileRequest, CreateFileRequestBody,
CreateMessageRequest, CreateMessageRequestBody,
)
file_path = Path(args.file)
if not file_path.exists():
_output({"error": f"File not found: {args.file}"})
sys.exit(1)
config, client = _build_lark_client()
suffix = file_path.suffix.lower()
file_type = "opus" if suffix in (".opus", ".ogg") else "stream"
msg_type = "audio" if file_type == "opus" else "file"
with open(file_path, "rb") as f:
body = CreateFileRequestBody.builder() \
.file_type(file_type) \
.file_name(file_path.name) \
.file(f)
if args.duration:
body = body.duration(args.duration)
body = body.build()
upload_req = CreateFileRequest.builder() \
.request_body(body) \
.build()
upload_resp = client.im.v1.file.create(upload_req)
if not upload_resp.success():
_output({"error": f"Upload failed: {upload_resp.msg}",
"code": upload_resp.code})
sys.exit(1)
file_key = upload_resp.data.file_key
content = json.dumps({"file_key": file_key})
msg_body = CreateMessageRequestBody.builder() \
.receive_id(args.chat_id) \
.msg_type(msg_type) \
.content(content) \
.build()
msg_req = CreateMessageRequest.builder() \
.receive_id_type("chat_id") \
.request_body(msg_body) \
.build()
msg_resp = client.im.v1.message.create(msg_req)
if msg_resp.success():
mid = msg_resp.data.message_id if msg_resp.data else None
_output({"message_id": mid, "file_key": file_key,
"msg_type": msg_type})
else:
_output({"error": f"Send failed: {msg_resp.msg}",
"code": msg_resp.code})
sys.exit(1)
except SystemExit:
raise
except Exception as e:
_output({"error": str(e)})
sys.exit(1)
return
if args.command == "send-image":
try:
from lark_oapi.api.im.v1 import (
CreateImageRequest, CreateImageRequestBody,
CreateMessageRequest, CreateMessageRequestBody,
)
file_path = Path(args.file)
if not file_path.exists():
_output({"error": f"File not found: {args.file}"})
sys.exit(1)
config, client = _build_lark_client()
with open(file_path, "rb") as f:
body = CreateImageRequestBody.builder() \
.image_type("message") \
.image(f) \
.build()
upload_req = CreateImageRequest.builder() \
.request_body(body) \
.build()
upload_resp = client.im.v1.image.create(upload_req)
if not upload_resp.success():
_output({"error": f"Image upload failed: {upload_resp.msg}",
"code": upload_resp.code})
sys.exit(1)
image_key = upload_resp.data.image_key
content = json.dumps({"image_key": image_key})
msg_body = CreateMessageRequestBody.builder() \
.receive_id(args.chat_id) \
.msg_type("image") \
.content(content) \
.build()
msg_req = CreateMessageRequest.builder() \
.receive_id_type("chat_id") \
.request_body(msg_body) \
.build()
msg_resp = client.im.v1.message.create(msg_req)
if msg_resp.success():
mid = msg_resp.data.message_id if msg_resp.data else None
_output({"message_id": mid, "image_key": image_key})
else:
_output({"error": f"Send failed: {msg_resp.msg}",
"code": msg_resp.code})
sys.exit(1)
except SystemExit:
raise
except Exception as e:
_output({"error": str(e)})
sys.exit(1)
return
if __name__ == "__main__":
main()