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/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) 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()) diff --git a/bot/bot.py b/bot/bot.py index 59a7887..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, file=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") @@ -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: - request.method = "POST" - request.files = {"file": file} - - return self.http_session.send(request.prepare(), timeout=self.timeout_s) + # Detects if file contents provided + if 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, 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..389616e --- /dev/null +++ b/example/file_bot.py @@ -0,0 +1,27 @@ +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 from File + with open('logo_bot.png', 'rb') as bf: + 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") + + + +bot.dispatcher.add_handler(MessageHandler(callback=message_cb)) +bot.start_polling() +bot.idle()