Skip to content

Возможность отправки любых данных #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ build/
# Mystic etc.
/etc/

# IntelliJ IDEA.
# IntelliJ IDEA. / VSCode
/.idea/
/out/
*.iml
.vscode/

# Mac OS useless files.
.DS_Store
Expand All @@ -29,4 +30,10 @@ Thumbs.db
desktop.ini

# logs
log/
log/

# Env
.env

# Dev mode
mailru_im_bot.egg-info
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<img src="logo_bot.png" width="100" height="100">

> 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)
Expand Down
2 changes: 1 addition & 1 deletion bot/__init__.py
Original file line number Diff line number Diff line change
@@ -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())
12 changes: 6 additions & 6 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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):
Expand Down
27 changes: 27 additions & 0 deletions example/file_bot.py
Original file line number Diff line number Diff line change
@@ -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()