Skip to content
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

Added file_name #34

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,33 @@ This library provides complete Bot API 1.0 interface and compatible with Python
# Installing
Install using pip:
```bash
pip install --upgrade mailru-im-bot
pip install --upgrade mailru-im-bot-updated
```

Install from sources:
```bash
git clone https://github.com/mail-ru-im/bot-python.git
cd bot-python
git clone https://github.com/Lunatik-cyber/bot-python-icq.git
cd icq_bot-python-icq
python setup.py install
```

# New features
* Added ***file_name*** argument to **send_file** function. Now you can send files with custom names.
* Now you can receive sent data in ***send_text, send_file, send_voice, edit_text functions,
answer_callback_query***.

### Example file_name argument
```python
bot.send_file(chat_id=chat_id, file_name="test.txt", file=open("test.txt", "rb"))
```

### Example sent data
```python
msg = bot.send_text(chat_id=chat_id, text="test")
bot.edit_text(chat_id=chat_id, msg_id=msg["msgId"], text="test2")
```


# API description
<ul>
<li><a href="https://icq.com/botapi/">icq.com/botapi/</a></li>
Expand Down
2 changes: 1 addition & 1 deletion example/decorators_bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bot.bot import Bot
from icq_bot.bot import Bot


TOKEN = "" # your token here
Expand Down
4 changes: 2 additions & 2 deletions example/echo_bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bot.bot import Bot
from bot.handler import MessageHandler
from icq_bot.bot import Bot
from icq_bot.handler import MessageHandler

TOKEN = "" #your token here

Expand Down
8 changes: 4 additions & 4 deletions example/format_bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from bot.bot import Bot
from bot.handler import MessageHandler
from bot.constant import ParseMode
from bot.types import Format
from icq_bot.bot import Bot
from icq_bot.handler import MessageHandler
from icq_bot.constant import ParseMode
from icq_bot.types import Format

import logging.config

Expand Down
4 changes: 2 additions & 2 deletions example/myteam_bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bot.bot import Bot
from bot.handler import MessageHandler
from icq_bot.bot import Bot
from icq_bot.handler import MessageHandler

import logging.config

Expand Down
2 changes: 1 addition & 1 deletion example/scheduled_bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import schedule
import time
from bot.bot import Bot
from icq_bot.bot import Bot

TOKEN = "" #your toke here
bot = Bot(token=TOKEN)
Expand Down
20 changes: 10 additions & 10 deletions example/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from os import path
from time import sleep
import sys
from bot.bot import Bot
from bot.filter import Filter
from bot.handler import HelpCommandHandler, UnknownCommandHandler, MessageHandler, FeedbackCommandHandler, \
from icq_bot.bot import Bot
from icq_bot.filter import Filter
from icq_bot.handler import HelpCommandHandler, UnknownCommandHandler, MessageHandler, FeedbackCommandHandler, \
CommandHandler, NewChatMembersHandler, LeftChatMembersHandler, PinnedMessageHandler, UnPinnedMessageHandler, \
EditedMessageHandler, DeletedMessageHandler, StartCommandHandler, BotButtonCommandHandler

Expand Down Expand Up @@ -88,7 +88,7 @@ def deleted_message_cb(bot, event):


def message_with_bot_mention_cb(bot, event):
bot.send_text(chat_id=event.data['chat']['chatId'], text="Message with bot mention was received")
bot.send_text(chat_id=event.data['chat']['chatId'], text="Message with icq_bot mention was received")


def mention_cb(bot, event):
Expand Down Expand Up @@ -198,7 +198,7 @@ def buttons_answer_cb(bot, event):


def main():
# Creating a new bot instance.
# Creating a new icq_bot instance.
bot = Bot(token=TOKEN, name=NAME, version=VERSION, api_url_base=API_URL)

# Registering handlers #
Expand Down Expand Up @@ -243,7 +243,7 @@ def main():
# Handler for deleted message
bot.dispatcher.add_handler(DeletedMessageHandler(callback=deleted_message_cb))

# Handler for message with bot mention
# Handler for message with icq_bot mention
bot.dispatcher.add_handler(MessageHandler(
filters=Filter.message & Filter.mention(user_id=bot.uin),
callback=message_with_bot_mention_cb
Expand Down Expand Up @@ -295,9 +295,9 @@ def main():
# ---------------------------------------------------------------------------------------- #
bot.start_polling()

# Call bot methods
# Call icq_bot methods
# -------------- #
# Get info about bot
# Get info about icq_bot
bot.self_get()

# Send message
Expand Down Expand Up @@ -389,7 +389,7 @@ def main():
# Set chat title
bot.set_chat_rules(chat_id=TEST_CHAT, rules="TEST RULES")

# Send bot buttons
# Send icq_bot buttons
bot.send_text(chat_id=OWNER,
text="Hello with buttons.",
inline_keyboard_markup="[{}]".format(json.dumps([
Expand All @@ -398,7 +398,7 @@ def main():
{"text": "Action 3", "callbackData": "call_back_id_3"}
])))

# Handler for bot buttons reply.
# Handler for icq_bot buttons reply.
bot.dispatcher.add_handler(BotButtonCommandHandler(callback=buttons_answer_cb))

bot.idle()
Expand Down
4 changes: 2 additions & 2 deletions bot/__init__.py → icq_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

__version__ = "0.0.21"
__version__ = "0.0.25"

# Set default logging handler to avoid "No handler found" warnings.
# Set default logging handler to avoid "No handler found" warnings.
logging.getLogger(__name__).addHandler(logging.NullHandler())
20 changes: 13 additions & 7 deletions bot/bot.py → icq_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def keyboard_to_json(keyboard_markup):
else:
return keyboard_markup


def format_to_json(format_):
if isinstance(format_, Format):
return format_.to_json()
Expand All @@ -40,6 +41,7 @@ def format_to_json(format_):
else:
return format_


class Bot(object):
def __init__(self, token: str, api_url_base: str = None, name: str = None, version: str = None,
timeout_s: int = 20, poll_time_s: int = 60, is_myteam: bool = False):
Expand Down Expand Up @@ -293,9 +295,10 @@ def send_text(self, chat_id: str, text: str, reply_msg_id=None, forward_chat_id=
"format": format_to_json(format_)
},
timeout=self.timeout_s
)
).json()

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, file=None, file_name=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 @@ -319,9 +322,12 @@ def send_file(self, chat_id, file_id=None, file=None, caption=None, reply_msg_id
)
if file:
request.method = "POST"
request.files = {"file": file}
if file_name:
request.files = {"file": (file_name, file)}
else:
request.files = {"file": file}

return self.http_session.send(request.prepare(), timeout=self.timeout_s)
return self.http_session.send(request.prepare(), timeout=self.timeout_s).json()

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 All @@ -343,7 +349,7 @@ def send_voice(self, chat_id, file_id=None, file=None, reply_msg_id=None, forwar
request.method = "POST"
request.files = {"file": file}

return self.http_session.send(request.prepare(), timeout=self.timeout_s)
return self.http_session.send(request.prepare(), timeout=self.timeout_s).json()

def edit_text(self, chat_id, msg_id, text, inline_keyboard_markup=None, parse_mode=None, format_=None):
if parse_mode and format_:
Expand All @@ -362,7 +368,7 @@ def edit_text(self, chat_id, msg_id, text, inline_keyboard_markup=None, parse_mo
"format": format_to_json(format_)
},
timeout=self.timeout_s
)
).json()

def delete_messages(self, chat_id, msg_id):
return self.http_session.get(
Expand All @@ -385,7 +391,7 @@ def answer_callback_query(self, query_id, text, show_alert=False, url=None):
"showAlert": 'true' if show_alert else 'false',
"url": url
}
)
).json()

def send_actions(self, chat_id, actions):
return self.http_session.get(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from setuptools import setup, find_packages

import bot
import icq_bot

here = path.abspath(path.dirname(__file__))

Expand All @@ -19,12 +19,12 @@ def requirements():


setup(
name="mailru-im-bot",
version=bot.__version__,
name="mailru-im-bot-updated",
version=icq_bot.__version__,
description="Pure Python interface for Bot API. Bot cookbook for Humans.",
long_description=long_description(),
long_description_content_type="text/markdown",
url="https://github.com/mail-ru-im/bot-python",
url="https://github.com/Lunatik-cyber/bot-python-icq",
author="ICQ LLC (Mail.Ru Group)",
author_email="[email protected]",
license="MIT",
Expand All @@ -45,7 +45,7 @@ def requirements():
"Programming Language :: Python :: 3.10",
"Operating System :: OS Independent"
],
keywords="mailru im bot api",
keywords="mailru im icq_bot api",
packages=find_packages(exclude=["example"]),
install_requires=requirements(),
python_requires=">= 2.7, != 3.0.*, != 3.1.*, != 3.2.*, != 3.3.*",
Expand Down