mostr-zulip-bot/zulip/zulip/examples/upload-file
Anders Kaseorg aeb89bcae5 ruff: Enable lots of rules.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-11-11 16:16:17 -08:00

36 lines
927 B
Python
Executable file

#!/usr/bin/env python3
import argparse
from io import StringIO
from typing import IO, Any
import zulip
usage = """upload-file [options]
Upload a file, and print the corresponding URI.
Example: upload-file --file-path=cat.png
Specify your Zulip API credentials and server in a ~/.zuliprc file or using the options.
If no --file-path is specified, a placeholder text file will be used instead.
"""
parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage))
parser.add_argument("--file-path", required=True)
options = parser.parse_args()
client = zulip.init_from_options(options)
if options.file_path:
file: IO[Any] = open(options.file_path, "rb") # noqa: SIM115
else:
file = StringIO("This is a test file.")
file.name = "test.txt"
response = client.upload_file(file)
try:
print("File URI: {}".format(response["uri"]))
except KeyError:
print(f"Error! API response was: {response}")