diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 33545d4d2..2b8762204 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -481,8 +481,9 @@ def set_sticker_set_thumbnail(token, name, user_id, thumbnail, format): def replace_sticker_in_set(token, user_id, name, old_sticker, sticker): method_url = r'replaceStickerInSet' - payload = {'user_id': user_id, 'name': name, 'old_sticker': old_sticker, 'sticker': sticker.to_json()} - return _make_request(token, method_url, params=payload) + json_dict, files = sticker.convert_input_sticker() + payload = {'user_id': user_id, 'name': name, 'old_sticker': old_sticker, 'sticker': json_dict} + return _make_request(token, method_url, params=payload, files=files, method='post') def set_chat_sticker_set(token, chat_id, sticker_set_name): method_url = r'setChatStickerSet' @@ -573,7 +574,7 @@ def send_checklist( reply_parameters=None, reply_markup=None ): method_url = r'sendChecklist' - payload = {'chat_id': chat_id, 'checklist': checklist, 'business_connection_id': business_connection_id} + payload = {'chat_id': chat_id, 'checklist': checklist.to_json(), 'business_connection_id': business_connection_id} if disable_notification is not None: payload['disable_notification'] = disable_notification if protect_content is not None: @@ -592,7 +593,7 @@ def edit_message_checklist( reply_markup=None ): method_url = r'editMessageChecklist' - payload = {'chat_id': chat_id, 'message_id': message_id, 'checklist': checklist, 'business_connection_id': business_connection_id} + payload = {'chat_id': chat_id, 'message_id': message_id, 'checklist': checklist.to_json(), 'business_connection_id': business_connection_id} if reply_markup: payload['reply_markup'] = _convert_markup(reply_markup) return _make_request(token, method_url, params=payload) diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index a90935864..918b13742 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -445,8 +445,9 @@ async def get_chat_member_count(token, chat_id): async def replace_sticker_in_set(token, user_id, name, old_sticker, sticker): method_url = r'replaceStickerInSet' - payload = {'user_id': user_id, 'name': name, 'old_sticker': old_sticker, 'sticker': sticker.to_json()} - return await _process_request(token, method_url, params=payload) + json_dict, files = sticker.convert_input_sticker() + payload = {'user_id': user_id, 'name': name, 'old_sticker': old_sticker, 'sticker': json_dict} + return await _process_request(token, method_url, params=payload, files=files, method='post') async def set_sticker_set_thumbnail(token, name, user_id, thumbnail, format): method_url = r'setStickerSetThumbnail' @@ -576,7 +577,7 @@ async def send_checklist( reply_parameters=None, reply_markup=None ): method_url = r'sendChecklist' - payload = {'chat_id': chat_id, 'checklist': checklist, 'business_connection_id': business_connection_id} + payload = {'chat_id': chat_id, 'checklist': checklist.to_json(), 'business_connection_id': business_connection_id} if disable_notification is not None: payload['disable_notification'] = disable_notification if protect_content is not None: @@ -595,7 +596,7 @@ async def edit_message_checklist( reply_markup=None ): method_url = r'editMessageChecklist' - payload = {'chat_id': chat_id, 'message_id': message_id, 'checklist': checklist, 'business_connection_id': business_connection_id} + payload = {'chat_id': chat_id, 'message_id': message_id, 'checklist': checklist.to_json(), 'business_connection_id': business_connection_id} if reply_markup: payload['reply_markup'] = await _convert_markup(reply_markup) return await _process_request(token, method_url, params=payload) diff --git a/tests/test_apihelper_95.py b/tests/test_apihelper_95.py index 80e1d26f4..ea26d057d 100644 --- a/tests/test_apihelper_95.py +++ b/tests/test_apihelper_95.py @@ -1,6 +1,9 @@ +import asyncio +import io +import json from types import SimpleNamespace -from telebot import apihelper +from telebot import apihelper, asyncio_helper, types def test_get_multipart_header_formatter_prefers_current_urllib3_name(): @@ -94,3 +97,62 @@ def fake_make_request(token, method_url, params=None, method=None, **kwargs): assert captured['params']['chat_id'] == 1 assert captured['params']['user_id'] == 2 assert captured['params']['tag'] == 'member-tag' + + +def _capture(captured): + def fake_request(token, method_url, params=None, files=None, method=None, **kwargs): + captured.update(method_url=method_url, params=params, files=files, method=method) + return {'ok': True} + return fake_request + + +def _checklist(): + return types.InputChecklist(title='Groceries', tasks=[types.InputChecklistTask(id=1, text='Milk')]) + + +def test_send_and_edit_checklist_send_checklist_as_json(monkeypatch): + captured = {} + monkeypatch.setattr(apihelper, '_make_request', _capture(captured)) + + apihelper.send_checklist('token', 'bc', 1, _checklist()) + assert json.loads(captured['params']['checklist'])['title'] == 'Groceries' + + apihelper.edit_message_checklist('token', 'bc', 1, 2, _checklist()) + assert json.loads(captured['params']['checklist'])['tasks'][0]['text'] == 'Milk' + + +def test_replace_sticker_in_set_uploads_the_sticker_file(monkeypatch): + captured = {} + monkeypatch.setattr(apihelper, '_make_request', _capture(captured)) + sticker_file = types.InputFile(io.BytesIO(b'sticker'), file_name='sticker.webp') + sticker = types.InputSticker(sticker=sticker_file, emoji_list=['\U0001f642'], format='static') + + apihelper.replace_sticker_in_set('token', 1, 'set_by_bot', 'old_id', sticker) + + attach = json.loads(captured['params']['sticker'])['sticker'] + assert attach.startswith('attach://') + assert captured['files'] == {attach[len('attach://'):]: sticker_file} + assert captured['method'] == 'post' + + +def test_async_checklist_and_replace_sticker(monkeypatch): + captured = {} + + async def fake_process_request(token, url, method='get', params=None, files=None, **kwargs): + captured.update(url=url, params=params, files=files, method=method) + return True + + monkeypatch.setattr(asyncio_helper, '_process_request', fake_process_request) + + asyncio.run(asyncio_helper.send_checklist('token', 'bc', 1, _checklist())) + assert json.loads(captured['params']['checklist'])['title'] == 'Groceries' + + asyncio.run(asyncio_helper.edit_message_checklist('token', 'bc', 1, 2, _checklist())) + assert json.loads(captured['params']['checklist'])['title'] == 'Groceries' + + sticker_file = types.InputFile(io.BytesIO(b'sticker'), file_name='sticker.webp') + sticker = types.InputSticker(sticker=sticker_file, emoji_list=['\U0001f642'], format='static') + asyncio.run(asyncio_helper.replace_sticker_in_set('token', 1, 'set_by_bot', 'old_id', sticker)) + attach = json.loads(captured['params']['sticker'])['sticker'] + assert captured['files'] == {attach[len('attach://'):]: sticker_file} + assert captured['method'] == 'post'