def send(self, bot: Bot, chat_id: Union[int, None] = None, reply_id: Union[int, None] = None): return bot.send_media_group(chat_id=chat_id, media=this.media, **MessageWithinChat.kwargs(self))
def send(self, bot: Bot, chat_id: Union[int, None] = None, reply_id: Union[int, None] = None): return bot.send_message( text=this.text, chat_id=chat_id, reply_to_message_id=reply_id, **MessageMixinParseMode.kwargs(self), **MessageWithinChat.kwargs(self), )
def send_post(submission, r2t): bot = r2t.telepot_bot bot_old = bot bot = Bot(bot_old._token) what, gif_url, ext = get_url(submission) if what != 'gif': return False title = submission.title link = submission.shortlink if submission.over_18: url = submission.url text = '{emoji}NSFW\n{url}\n{title}\n\n{link}\n\nby {channel}'.format( emoji=NSFW_emoji, url=url, title=title, link=link, channel=t_channel) bot.send_message(t_channel, text, disable_web_page_preview=True) return True text = '{title}\n{link}\n\nby {channel}'.format(title=title, link=link, channel=t_channel) logger.info("{channel} Posting {gif_url}:\n{text}".format( channel=t_channel, gif_url=gif_url, text=text)) try: # try sending as gif return send_gif(bot, t_channel, gif_url, caption=text, ext=ext) except Exception: # sending it link-only text = '{url}\n{title}\n\n{link}\n\nby {channel}'.format( url=gif_url, title=title, link=link, channel=t_channel) bot.send_message(t_channel, text, disable_web_page_preview=False) return True
__all__ = [] from typing import Union, AnyStr from pytgbot.bot import Bot bot = Bot('asd') class MessageBase(object): """ This is the base class of all the Message types in this class """ pass # end class class MessageMixin(MessageBase): """ base class for all mixins """ pass # end class class MessageWithChatID(MessageBase): def __init__( self, chat_id: Union[str, int] = None, ): """ Parameters:
def setUp(self): self.bot = APIBot(API_KEY) self.messages = [] self.messages.append( self.bot.send_message(TEST_CHAT, 'Unittest started.')) self.reply_to = self.messages[0].message_id
class MessageTypesRealSendingTest(unittest.TestCase): def setUp(self): self.bot = APIBot(API_KEY) self.messages = [] self.messages.append( self.bot.send_message(TEST_CHAT, 'Unittest started.')) self.reply_to = self.messages[0].message_id # end def def test_send_media_group(self): media = [ InputMediaPhoto(pic1, caption='1'), InputMediaPhoto(InputFileFromURL(pic1), caption='2'), InputMediaVideo(vid1, caption='3'), InputMediaVideo(InputFileFromURL(vid1), thumb=tmb1, caption='4'), InputMediaVideo(InputFileFromURL(vid1), thumb=InputFileFromURL(tmb1), caption='5'), ] msgs = self.bot.send_media_group( TEST_CHAT, media, disable_notification=True, ) self.messages.extend(msgs) self.messages.append( self.bot.send_message(TEST_CHAT, 'done with unittest {}.'.format(self.id()))) # end def def test_media_group(self): media = [ InputMediaPhoto(pic1, caption='1'), InputMediaPhoto(InputFileFromURL(pic1), caption='2'), InputMediaVideo(vid1, caption='3'), InputMediaVideo(InputFileFromURL(vid1), thumb=tmb1, caption='4'), InputMediaVideo(InputFileFromURL(vid1), thumb=InputFileFromURL(tmb1), caption='5'), ] m = MediaGroupMessage(media, disable_notification=True) msg = m.send(self.bot, TEST_CHAT, self.reply_to) self.messages.extend(msg) self.messages.append( self.bot.send_message(TEST_CHAT, 'done with unittest {}.'.format(self.id()))) # end def def test_photo_url(self): m = PhotoMessage(file_url=pic1, disable_notification=True) msg = m.send(self.bot, TEST_CHAT, self.reply_to) self.assertIsNotNone(msg) self.messages.append(msg) self.messages.append( self.bot.send_message(TEST_CHAT, 'done with unittest {}.'.format(self.id()))) # end def def test_photo_file_id(self): photo_msg = self.bot.send_photo(TEST_CHAT, photo=pic2, caption='this has a file_id') self.messages.append(photo_msg) file_id = self._get_biggest_photo_fileid(photo_msg) m = PhotoMessage(file_id=file_id, disable_notification=True) msg = m.send(self.bot, TEST_CHAT, self.reply_to) self.assertIsNotNone(msg) self.messages.append(msg) self.messages.append( self.bot.send_message(TEST_CHAT, 'done with unittest {}.'.format(self.id()))) # end def def test_sticker(self): sticker_msg = self.bot.send_sticker(TEST_CHAT, sticker=stk1) self.messages.append(sticker_msg) m = StickerMessage(file_id=stk1, disable_notification=True) msg = m.send(self.bot, TEST_CHAT, self.reply_to) self.assertIsNotNone(msg) self.messages.append(msg) self.messages.append( self.bot.send_message(TEST_CHAT, 'done with unittest {}.'.format(self.id()))) print('lel') # end def # # utils: # def _get_biggest_photo_fileid(self, msg): biggest = msg.photo[0] for photo in msg.photo: self.assertIsInstance(photo, PhotoSize) if photo.file_size > biggest.file_size: biggest = photo # end if # end for return biggest.file_id # end def def tearDown(self): if self.bot and self.messages: for msg in reversed(self.messages): try: self.bot.delete_message(TEST_CHAT, msg.message_id) except TgApiServerException as e: if e.error_code == 400 and e.description == 'Bad Request: message to delete not found': logger.info('delete message fail, not found.') continue # end if logger.debug('delete message fail.', exc_info=True) # end try # end for # end if self.messages = []
def send(self, bot: Bot, chat_id: Union[int, None] = None, reply_id: Union[int, None] = None): return bot.send_chat_action(chat_id=self.chat_id, action=self.action)
def setUp(self): self.bot = Bot(API_KEY) self.messages = []
class BotTest(unittest.TestCase): def setUp(self): self.bot = Bot(API_KEY) self.messages = [] # end def def test_edit_message_media(self): # upload by url url1 = 'https://derpicdn.net/img/view/2012/1/22/1382.jpg' url2 = 'https://derpicdn.net/img/view/2016/2/3/1079240.png' msg = self.bot.send_photo(TEST_CHAT, url1, caption="unittest", disable_notification=True) self.messages.append(msg) print("msg 1: {!r}".format(msg)) self.assertIsInstance(msg, Message) self.assertEqual(msg.caption, 'unittest') self.assertIn('photo', msg) self.assertIsInstance(msg.photo, list) msg_id = msg.message_id file_id = self._get_biggest_photo_fileid(msg) # edit by url msg2 = self.bot.edit_message_media(InputMediaPhoto(url2), TEST_CHAT, message_id=msg_id) self.messages.append(msg2) print("msg 2: {!r}".format(msg2)) self.assertIsInstance(msg2, Message) self.assertIn('photo', msg2) self.assertIsInstance(msg2.photo, list) self.assertEqual(msg2.caption, None) file_id2 = self._get_biggest_photo_fileid(msg2) # edit by file_id msg3 = self.bot.edit_message_media(InputMediaPhoto(file_id), TEST_CHAT, message_id=msg_id) self.messages.append(msg3) print("msg 3: {!r}".format(msg3)) self.assertIsInstance(msg3, Message) self.assertIn('photo', msg3) self.assertIsInstance(msg3.photo, list) file_id3 = self._get_biggest_photo_fileid(msg3) self.assertEqual(msg2.caption, None) self.assertEqual(file_id3, file_id) # edit by upload (url) msg4 = self.bot.edit_message_media(InputMediaPhoto( InputFileFromURL(url2)), TEST_CHAT, message_id=msg_id) self.messages.append(msg4) print("msg 4: {!r}".format(msg4)) self.assertIsInstance(msg4, Message) self.assertIn('photo', msg4) self.assertIsInstance(msg4.photo, list) self.assertEqual(msg4.caption, None) file_id4 = self._get_biggest_photo_fileid(msg4) self.messages.append(self.bot.send_message(TEST_CHAT, 'done.')) # end def def test_send_media_group(self): url1 = 'https://derpicdn.net/img/view/2012/1/22/1382.jpg' url2 = 'https://derpicdn.net/img/view/2016/2/3/1079240.png' vid1 = 'https://derpicdn.net/img/view/2016/12/21/1322277.mp4' pic1 = 'https://derpicdn.net/img/2017/7/21/1491832/thumb.jpeg' stuff = [ InputMediaPhoto(url1, caption='1'), InputMediaPhoto(InputFileFromURL(url1), caption='2'), InputMediaVideo(vid1, caption='3'), InputMediaVideo(InputFileFromURL(vid1), thumb=pic1, caption='4'), InputMediaVideo(InputFileFromURL(vid1), thumb=InputFileFromURL(pic1), caption='5'), ] msgs = self.bot.send_media_group( TEST_CHAT, stuff, disable_notification=True, ) self.messages.extend(msgs) self.messages.append(self.bot.send_message(TEST_CHAT, 'done.')) # end def # # utils: # def _get_biggest_photo_fileid(self, msg): biggest = msg.photo[0] for photo in msg.photo: self.assertIsInstance(photo, PhotoSize) if photo.file_size > biggest.file_size: biggest = photo # end if # end for return biggest.file_id # end def def tearDown(self): if self.bot and self.messages: for msg in reversed(self.messages): try: self.bot.delete_message(TEST_CHAT, msg.message_id) except TgApiServerException as e: if e.error_code == 400 and e.description == 'Bad Request: message to delete not found': logger.info('delete message fail, not found.') continue # end if logger.debug('delete message fail.', exc_info=True) # end try # end for # end if self.messages = []
def process_audio(api_key, chat_id, progress_msg_id, message_id, language_code, file_id, mime_type, duration, title=None, performer=None): bot = Bot(api_key) username = bot.get_me().username assert isinstance(bot, Bot) ln = l(language_code) #if audio.duration > 210 or audio.file_size > 7000000: # 3.5 minutes # bot.edit_message_text( # ln.file_too_big, chat_id, progress_msg_id, disable_web_page_preview=True # ) # return ## end if try: logger.debug("Mime is {mime}".format(mime=mime_type)) if mime_type not in AUDIO_FORMATS: logger.debug("Mime is wrong") bot.edit_message_text( ln.format_unsupported, chat_id, progress_msg_id, disable_web_page_preview=True ) return # end if logger.debug("Downloading file: {id}".format(id=file_id)) file_in = bot.get_file(file_id) url = bot.get_download_url(file_in) logger.debug("Downloading file: {url}".format(url=url)) r = requests.get(url, stream=True) logger.debug("Downloaded file.") fake_file_in = BytesIO(r.content) fake_file_out = BytesIO() audio_format = AUDIO_FORMATS[mime_type] logger.debug("Format is {format}".format(format=audio_format)) audio_in = AudioSegment.from_file(fake_file_in, format=audio_format) audio_out = None logger.debug("Calling boost()...") for step in boost(audio_in): if isinstance(step, int): text = getattr(ln, "progress" + str(step)) logger.debug("Progress {step}: {text}".format(step=step, text=text)) try: bot.edit_message_text( text, chat_id, progress_msg_id, disable_web_page_preview=True ) except TgApiServerException: logger.exception("Editing status message failed") # end try # end if else: audio_out = step # end for bot.send_chat_action(chat_id, "record_audio") # end def logger.debug("Done with boost()...") assert_type_or_raise(audio_out, AudioSegment) assert isinstance(audio_out, AudioSegment) bot.send_chat_action(chat_id, "upload_audio") bot_link = "https://t.me/{bot}".format(bot=username) tags = { "composer": bot_link, "service_name": bot_link, "comment": "TEST°!!!", "genre": "BOOSTED BASS", "encoder": "Horseapples 1.2 - {bot_link} (littlepip is best pony/)".format(bot_link=bot_link), "encoded_by": bot_link } if title: tags["title"] = title # end if if performer: tags["artist"] = performer # end if audio_out.export(fake_file_out, format="mp3",tags=tags) file_out = InputFileFromBlob( file_blob=fake_file_out.getvalue(), file_mime="audio/mpeg", file_name="bass boosted by @{bot}.mp3".format(bot=username), ) bot.send_chat_action(chat_id, "upload_audio") caption = ln.caption.format(bot=username) logger.debug("uploading new audio") bot.send_audio( chat_id, file_out, caption=caption, duration=duration, performer=performer, title=title, disable_notification=False, reply_to_message_id=message_id ) logger.debug("deleting status message") bot.delete_message(chat_id, progress_msg_id) except Exception as e: logger.exception("Got Exeption instead of bass!") bot.edit_message_text( ln.generic_error, chat_id, progress_msg_id, disable_web_page_preview=True )