From 338d3613fa7f1f567cbca6ab32a6d255220cb32c Mon Sep 17 00:00:00 2001 From: S1em Date: Tue, 13 May 2025 09:03:11 +0000 Subject: [PATCH 1/4] feat: Add ability to send any data with filename --- .gitignore | 11 +++++++++-- bot/bot.py | 10 +++++----- example/file_bot.py | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 example/file_bot.py diff --git a/.gitignore b/.gitignore index a3618a5..843f1c0 100644 --- a/.gitignore +++ b/.gitignore @@ -16,10 +16,11 @@ build/ # Mystic etc. /etc/ -# IntelliJ IDEA. +# IntelliJ IDEA. / VSCode /.idea/ /out/ *.iml +.vscode/ # Mac OS useless files. .DS_Store @@ -29,4 +30,10 @@ Thumbs.db desktop.ini # logs -log/ \ No newline at end of file +log/ + +# Env +.env + +# Dev mode +mailru_im_bot.egg-info \ No newline at end of file diff --git a/bot/bot.py b/bot/bot.py index 59a7887..9b7e1b2 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -306,7 +306,7 @@ def send_text(self, chat_id: str, text: str, reply_msg_id=None, forward_chat_id= timeout=self.timeout_s ) - def send_file(self, chat_id, file_id=None, file=None, caption=None, reply_msg_id=None, forward_chat_id=None, + def send_file(self, chat_id, file_id=None, filename='file.unknown', file_content=None, caption=None, reply_msg_id=None, forward_chat_id=None, forward_msg_id=None, inline_keyboard_markup=None, parse_mode=None, format_=None): if parse_mode and format_: raise Exception("Cannot use format and parseMode fields at one time") @@ -328,11 +328,11 @@ def send_file(self, chat_id, file_id=None, file=None, caption=None, reply_msg_id "format": format_to_json(format_) } ) - if file: + # Detects if file contents provided + if file_content: request.method = "POST" - request.files = {"file": file} - - return self.http_session.send(request.prepare(), timeout=self.timeout_s) + request.files = {'file': (filename, file_content)} + return self.http_session.send(request=request.prepare(),timeout=self.timeout_s) def send_voice(self, chat_id, file_id=None, file=None, reply_msg_id=None, forward_chat_id=None, forward_msg_id=None, inline_keyboard_markup=None): diff --git a/example/file_bot.py b/example/file_bot.py new file mode 100644 index 0000000..ffd3d43 --- /dev/null +++ b/example/file_bot.py @@ -0,0 +1,24 @@ +from bot.bot import Bot +from bot.handler import MessageHandler +import os + +TOKEN = os.getenv("VK_TOKEN") +URL = os.getenv("VK_URL") + +bot = Bot(token=TOKEN, api_url_base=URL) + + +def message_cb(bot: Bot, event): + # Send text data + bot.send_file(chat_id=event.from_chat,file_content="Some Text", filename="Text.txt") + # Send text with no name + bot.send_file(chat_id=event.from_chat,file_content="Some Text") + # Send binary data + with open('logo_bot.png', 'rb') as bf: + bot.send_file(chat_id=event.from_chat,file_content=bf.read()) + + + +bot.dispatcher.add_handler(MessageHandler(callback=message_cb)) +bot.start_polling() +bot.idle() From 1f4c1ac272eb86dc934697747492e06976d76402 Mon Sep 17 00:00:00 2001 From: S1em Date: Tue, 13 May 2025 10:21:52 +0000 Subject: [PATCH 2/4] feat: Use both methods (file|bytes) --- bot/bot.py | 6 +++--- example/file_bot.py | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 9b7e1b2..697585a 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -306,7 +306,7 @@ def send_text(self, chat_id: str, text: str, reply_msg_id=None, forward_chat_id= timeout=self.timeout_s ) - def send_file(self, chat_id, file_id=None, filename='file.unknown', file_content=None, caption=None, reply_msg_id=None, forward_chat_id=None, + def send_file(self, chat_id, file_id=None, filename=None, file_content=None, caption=None, reply_msg_id=None, forward_chat_id=None, forward_msg_id=None, inline_keyboard_markup=None, parse_mode=None, format_=None): if parse_mode and format_: raise Exception("Cannot use format and parseMode fields at one time") @@ -330,8 +330,8 @@ def send_file(self, chat_id, file_id=None, filename='file.unknown', file_content ) # Detects if file contents provided if file_content: - request.method = "POST" - request.files = {'file': (filename, file_content)} + request.method = 'POST' + request.files = {'file': (filename, file_content) if filename else file_content} return self.http_session.send(request=request.prepare(),timeout=self.timeout_s) def send_voice(self, chat_id, file_id=None, file=None, reply_msg_id=None, forward_chat_id=None, diff --git a/example/file_bot.py b/example/file_bot.py index ffd3d43..389616e 100644 --- a/example/file_bot.py +++ b/example/file_bot.py @@ -13,9 +13,12 @@ def message_cb(bot: Bot, event): bot.send_file(chat_id=event.from_chat,file_content="Some Text", filename="Text.txt") # Send text with no name bot.send_file(chat_id=event.from_chat,file_content="Some Text") - # Send binary data + # Send binary data from File with open('logo_bot.png', 'rb') as bf: - bot.send_file(chat_id=event.from_chat,file_content=bf.read()) + bot.send_file(chat_id=event.from_chat,file_content=bf) + # Send binary data from bytes + with open('logo_bot.png', 'rb') as bf: + bot.send_file(chat_id=event.from_chat,file_content=bf.read(), filename="My File.png") From 2b340097f57ad7ca21fdf637e18e96c5701058c8 Mon Sep 17 00:00:00 2001 From: S1em Date: Tue, 13 May 2025 11:23:34 +0000 Subject: [PATCH 3/4] Update version --- bot/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/__init__.py b/bot/__init__.py index 8774c3b..e064f47 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -1,6 +1,6 @@ import logging -__version__ = "0.0.22" +__version__ = "0.0.23" # Set default logging handler to avoid "No handler found" warnings. logging.getLogger(__name__).addHandler(logging.NullHandler()) From 2f8579e2b5053b7919c4b8d0367f7b4303b8086d Mon Sep 17 00:00:00 2001 From: S1em Date: Tue, 13 May 2025 12:08:13 +0000 Subject: [PATCH 4/4] Update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 76cd617..a715e0a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ +> This repo is a fork of original [mailru-im-bot](https://github.com/mail-ru-im/bot-python). + # VK Teams Bot API for Python [![Python package](https://github.com/mail-ru-im/bot-python/actions/workflows/python-package.yml/badge.svg)](https://github.com/mail-ru-im/bot-python/actions/workflows/python-package.yml) [![codecov](https://codecov.io/github/mail-ru-im/bot-python/graph/badge.svg?token=ObUFRWSyGv)](https://codecov.io/github/mail-ru-im/bot-python)