async def test(): telegram = Telegram(token="token", session=aiohttp.ClientSession()) telegram.api_messages_lock = asyncio.Lock() async def req(method, kwargs): requests.append((method, kwargs)) telegram._request = req attachment = Attachment.new(b"file") await telegram.execute_send(1, "", attachment, {}) attachment = Attachment.new(b"file", type="doc") await telegram.execute_send(1, "", attachment, {}) attachment = Attachment.new(b"file", type="voice") await telegram.execute_send(1, "", attachment, {}) assert len(requests) == 3 assert requests[0] == ("sendPhoto", {"chat_id": "1", "photo": b"file"}) assert requests[1] == ("sendDocument", { "chat_id": '1', "document": b'file' }) assert requests[2] == ("sendVoice", {"chat_id": '1', "voice": b'file'})
async def test(): attachment = Attachment.new(b"content") image = await vkontakte.upload_attachment(attachment, peer_id=123) assert image.type == "image" assert image.id is not None assert image.file is None attachment = Attachment.new(b"content", type="doc") doc = await vkontakte.upload_attachment(attachment, peer_id=123) assert doc.type == "doc" assert doc.id is not None assert doc.file is None attachment = Attachment.new(b"content", type="voice") voice = await vkontakte.upload_attachment(attachment, peer_id=123) assert voice.type == "voice" assert voice.id is not None assert voice.file is None attachment = Attachment.new(b"content", type="graffiti") voice = await vkontakte.upload_attachment(attachment, peer_id=123) assert voice.type == "graffiti" assert voice.id == "graffiti87641997_497831521" assert voice.file is None attachment = Attachment.new(b"content", type="video") with pytest.raises(ValueError): await vkontakte.upload_attachment(attachment, peer_id=123)
async def __(msg, ctx): # Document with open(get_path(__file__, "assets/pizza.png"), "rb") as fh: doc = Attachment.new(fh.read(), "pizza.png") await ctx.reply("Document", attachments=doc) # Audio message with open(get_path(__file__, "assets/audio.ogg"), "rb") as fh: audio_message = Attachment.new(fh.read(), "audio.ogg", "voice") await ctx.reply("Audio message", attachments=audio_message)
def test_upload_attachment_error_retry( mock_make_attachment, mock_upload_file_to_vk, mock_request, ): mock_request.side_effect = [ {"upload_url": "_"}, RequestException(None, None, None, {"error_code": 1}), {"upload_url": "_"}, "ok", ] mock_upload_file_to_vk.side_effect = [ "ok", "ok", ] mock_make_attachment.return_value = "ok" vkontakte = VkontakteLongpoll("token") result = asyncio.get_event_loop().run_until_complete( vkontakte.upload_attachment(Attachment.new(b""), peer_id=123) ) assert result == "ok"
async def test(): telegram = Telegram(token="token", session=aiohttp.ClientSession()) telegram.api_messages_lock = asyncio.Lock() attachment = Attachment.new(b"bruh", type="location") with pytest.raises(ValueError): await telegram.execute_send(1, "", attachment, {})
async def bg_loop(vk): while True: with open(get_path(__file__, "assets/pizza.png"), "rb") as fh: temp_a = Attachment.new(fh.read(), "pizza.png") a = await vk.upload_attachment(temp_a, peer_id=None) for sub in subscribers: await vk.send_message(sub, "", a) await asyncio.sleep(5)
async def _(msg, ctx): # Document with open(get_path(__file__, "assets/pizza.png"), "rb") as fh: doc = Attachment.new(fh.read(), "pizza.png") await ctx.reply("Document", attachments=doc) # Graffiti (special for vk) with open(get_path(__file__, "assets/pizza.png"), "rb") as fh: graffiti = Attachment.new(fh.read(), "pizza.png", type="graffiti") try: await ctx.reply("Graffiti", attachments=graffiti) except ValueError: await ctx.reply("Can't upload this type of attachments") # Audio message with open(get_path(__file__, "assets/audio.ogg"), "rb") as fh: audio_message = Attachment.new(fh.read(), "audio.ogg", "voice") await ctx.reply("Audio message", attachments=audio_message)
async def _(msg, ctx): files = files_find("/home/hord/Музыка/", ctx.body) if (len(files) == 0): await ctx.reply("Нет такой музыки...") else: filepath = files.pop() with open(get_path(__file__, filepath), "rb") as fh: audio_message = Attachment.new(fh.read(), os.path.basename(filepath), "voice") await ctx.reply(os.path.basename(filepath), attachments=audio_message)
async def test(): # image attachment = Attachment.new(b"content") image = await vkontakte.upload_attachment(attachment, peer_id=123) assert image.type == "image" assert image.id is not None assert image.file is None # doc with peer_id attachment = Attachment.new(b"content", type="doc") doc = await vkontakte.upload_attachment(attachment, peer_id=123) assert doc.type == "doc" assert doc.id is not None assert doc.file is None # doc without peer_id attachment = Attachment.new(b"content", type="doc") doc = await vkontakte.upload_attachment(attachment, peer_id=None) assert doc.type == "doc" assert doc.id is not None assert doc.file is None # voice attachment = Attachment.new(b"content", type="voice") voice = await vkontakte.upload_attachment(attachment, peer_id=123) assert voice.type == "voice" assert voice.id is not None assert voice.file is None # video attachment = Attachment.new(b"content", type="video") with pytest.raises(ValueError): await vkontakte.upload_attachment(attachment, peer_id=123)
async def _(msg, ctx): messages = await extract_messages(msg, ctx) if not messages: await ctx.reply('Вы не указали сообщения для цитаты') else: user_ids = [i["from_id"] for i in messages] await asyncio.sleep(0) # заставляем роботать другие процессы users = await get_avatars_and_names(ctx, user_ids) file = await make_alternate_quote(users, messages) if not file: await ctx.reply("Текста не найдено") return attach = Attachment.new(file) attachment = await ctx.backend.upload_attachment( attach, peer_id=msg.receiver_id) message = "" await ctx.reply(message, attachments=attachment)
def test_upload_attachment_error_no_retry( mock_upload_file_to_vk, mock_request, ): mock_request.side_effect = [ {"upload_url": "_"}, RequestException(None, None, None, {"error_code": 1}), ] mock_upload_file_to_vk.side_effect = [ "ok", ] vkontakte = VkontakteLongpoll("token") with pytest.raises(RequestException): asyncio.get_event_loop().run_until_complete( vkontakte.upload_attachment(Attachment.new(b"")) )
async def bg_loop(vk): while True: with open(get_path(__file__, "assets/pizza.png"), "rb") as fh: temp_a = Attachment.new(fh.read(), "pizza.png") try: a = await vk.upload_attachment(temp_a, peer_id=None) except RequestException: for sub in subscribers: await vk.send_message(sub, "Failed to upload attachment; Paused for 1 hour") await asyncio.sleep(60 * 60) continue for sub in subscribers: await vk.send_message(sub, "", a) await asyncio.sleep(60 * 10)
def test_execute_send_new(): vkontakte = VkontakteLongpoll(token="token") async def _upl_att(attachment, peer_id): return attachment._replace(id=1, raw={"ok": "ok"}) vkontakte.upload_attachment = _upl_att async def req(method, kwargs): assert method == "messages.send" assert kwargs["attachment"] == "1" return 1 vkontakte._request = req attachment = Attachment.new(b"content", "image") result = asyncio.get_event_loop().run_until_complete( vkontakte.execute_send(1, "text", attachment, {}) ) assert result == 1
async def __(msg, ctx): with open(get_path(__file__, "assets/pizza.png"), "rb") as fh: image = Attachment.new(fh.read(), "pizza.png") await ctx.reply("", attachments=image)