def send(self, sender: PytgbotApiBot) -> PytgbotApiMessage: self.prepare_file() assert isinstance(self.file, (InputFile, InputFileFromDisk, InputFileFromURL, str)) if not self.file_id and not any([self.file.file_name.endswith(x) for x in [".jpg", ".jpeg", ".gif", ".png", ".tif", ".bmp"]]): # set the suffix if self.file.file_mime in ["image/jpg", "image/jpeg", "image/jpe"]: # manually, to avoid .jpe ending. self.file.file_name += ".jpg" else: import mimetypes ext = mimetypes.guess_extension(self.file.file_mime) # automatically if ext not in [".jpg", ".jpeg", ".gif", ".png", ".tif", ".bmp"]: ext = ".unknown-file-type.png" # At least we can try setting it as .png self.file.file_name += ext # end if # end if try: return sender.send_photo( chat_id=self.receiver, photo=self.file, caption=self.caption, reply_to_message_id=self.reply_id, reply_markup=self.reply_markup, disable_notification = self.disable_notification ) except TgApiServerException as e: if e.error_code == 400 and e.description.startswith('bad request') and 'reply message not found' in e.description: logger.debug('Trying to resend without reply_to.') return sender.send_photo( chat_id=self.receiver, photo=self.file, caption=self.caption, reply_to_message_id=self.reply_id, reply_markup=self.reply_markup, disable_notification=self.disable_notification ) # end if raise # else it just raises as usual
def send(self, sender: PytgbotApiBot, receiver, reply_id)->PytgbotApiMessage: if self.receiver: receiver = self.receiver # end if if self.reply_id is not DEFAULT_MESSAGE_ID: reply_id = self.reply_id # end if self.prepare_file() assert isinstance(self.file, (InputFile, InputFileFromDisk, InputFileFromURL)) if not any([self.file.file_name.endswith(x) for x in [".jpg", ".jpeg", ".gif", ".png", ".tif", ".bmp"]]): if self.file.file_mime in ["image/jpg", "image/jpeg", "image/jpe"]: # manually, to avoid .jpe ending. self.file.file_name+=".jpg" else: import mimetypes ext = mimetypes.guess_extension(self.file.file_mime) # automatically if ext not in [".jpg", ".jpeg", ".gif", ".png", ".tif", ".bmp"]: ext = ".unknown-file-type.png" # At least we can try setting it as .png self.file.file_name += ext try: return sender.send_photo( receiver, self.file, caption=self.caption, reply_to_message_id=reply_id, reply_markup=self.reply_markup, disable_notification = self.disable_notification ) except TgApiServerException as e: should_backoff(e) # checks if it should raise an DoRetryException raise # else it just raises as usual
def send(self, sender: PytgbotApiBot) -> PytgbotApiMessage: self.prepare_file() assert isinstance( self.file, (InputFile, InputFileFromDisk, InputFileFromURL, str)) if not self.file_id and not any([ self.file.file_name.endswith(x) for x in [".jpg", ".jpeg", ".gif", ".png", ".tif", ".bmp"] ]): # set the suffix if self.file.file_mime in ["image/jpg", "image/jpeg", "image/jpe" ]: # manually, to avoid .jpe ending. self.file.file_name += ".jpg" else: import mimetypes ext = mimetypes.guess_extension( self.file.file_mime) # automatically if ext not in [ ".jpg", ".jpeg", ".gif", ".png", ".tif", ".bmp" ]: ext = ".unknown-file-type.png" # At least we can try setting it as .png self.file.file_name += ext # end if # end if try: return sender.send_photo( chat_id=self.receiver, photo=self.file, caption=self.caption, reply_to_message_id=self.reply_id, reply_markup=self.reply_markup, disable_notification=self.disable_notification) except TgApiServerException as e: should_backoff(e) # checks if it should raise an DoRetryException raise # else it just raises as usual
last_update_id = -1 while True: # loop forever. for update in bot.get_updates(limit=100, offset=last_update_id+1, poll_timeout=30): last_update_id = update.update_id print("got message: {msg}".format(msg=update)) if not "message" in update: continue message = update.message if not "text" in message: continue logger.debug("got text: {msg}".format(msg=message.text.encode("utf-8"))) if not message.text == "/pics": continue origin = message.chat.id if "chat" in message else message.from_peer.id if "reply_to_message" in message: name = message.reply_to_message.from_peer.first_name peer = message.reply_to_message.from_peer.id photos = bot.get_user_profile_photos(peer).photos if len(photos) == 0: bot.send_message(origin, "No images.") continue for x in reversed(photos): biggest_image = max(x, key=lambda p: p.file_size) # get the biggest image. bot.send_photo(origin, biggest_image.file_id, caption="{name}".format(name=name, w=biggest_image.width, h=biggest_image.height) ) else: bot.send_message(origin, "Please reply to some message to select a user.") # end if # end for #end while
from pytgbot import Bot from somewhere import API_KEY # just set the key manually. # API_KEY = "1231412:adLsIfTsTsLfEfPdIdPwdwIoSaBgEuSzTwszPdOaNdYs" bot = Bot(API_KEY) last_update_id = -1 while True: # loop forever. for update in bot.get_updates(limit=100, offset=last_update_id+1)["result"]: last_update_id = update["update_id"] if not "message" in update: continue message = update["message"] logger.info("got message: {msg}".format(msg=message)) if not "text" in message: continue logger.info("got message: {msg}".format(msg=message.text.encode("utf-8"))) if "reply_to_message" in message and message.text == "/pics": name = message.reply_to_message["from"].first_name peer = message.reply_to_message["from"].id origin = message.chat.id if "chat" in message else message["from"].id for x in reversed(bot.get_user_profile_photos(peer)["result"]["photos"]): biggest_image = max(x, key=lambda p: p["file_size"]) # get the biggest image. bot.send_photo(origin, biggest_image["file_id"], caption="{name}".format(name=name, w=biggest_image["width"], h=biggest_image["height"]) ) #end if # end for #end while
if not "message" in update: continue message = update.message if not "text" in message: continue logger.debug( "got text: {msg}".format(msg=message.text.encode("utf-8"))) if not message.text == "/pics": continue origin = message.chat.id if "chat" in message else message.from_peer.id if "reply_to_message" in message: name = message.reply_to_message.from_peer.first_name peer = message.reply_to_message.from_peer.id photos = bot.get_user_profile_photos(peer).photos if len(photos) == 0: bot.send_message(origin, "No images.") continue for x in reversed(photos): biggest_image = max( x, key=lambda p: p.file_size) # get the biggest image. bot.send_photo(origin, biggest_image.file_id, caption="{name}".format(name=name, w=biggest_image.width, h=biggest_image.height)) else: bot.send_message(origin, "Please reply to some message to select a user.") # end if # end for #end while