Python telegram.Update() Examples
The following are 30
code examples of telegram.Update().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
telegram
, or try the search function
.
Example #1
Source File: users.py From SkittBot with GNU General Public License v3.0 | 8 votes |
def log_user(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] msg = update.effective_message # type: Optional[Message] sql.update_user(msg.from_user.id, msg.from_user.username, chat.id, chat.title) if msg.reply_to_message: sql.update_user(msg.reply_to_message.from_user.id, msg.reply_to_message.from_user.username, chat.id, chat.title) if msg.forward_from: sql.update_user(msg.forward_from.id, msg.forward_from.username)
Example #2
Source File: global_bans.py From SkittBot with GNU General Public License v3.0 | 7 votes |
def enforce_gban(bot: Bot, update: Update): # Not using @restrict handler to avoid spamming - just ignore if cant gban. if sql.does_chat_gban(update.effective_chat.id) and update.effective_chat.get_member(bot.id).can_restrict_members: user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] msg = update.effective_message # type: Optional[Message] if user and not is_user_admin(chat, user.id): check_and_ban(update, user.id) if msg.new_chat_members: new_members = update.effective_message.new_chat_members for mem in new_members: check_and_ban(update, mem.id) if msg.reply_to_message: user = msg.reply_to_message.from_user # type: Optional[User] if user and not is_user_admin(chat, user.id): check_and_ban(update, user.id, should_message=False)
Example #3
Source File: blacklist.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def blacklist(bot: Bot, update: Update, args: List[str]): msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] all_blacklisted = sql.get_chat_blacklist(chat.id) filter_list = BASE_BLACKLIST_STRING if len(args) > 0 and args[0].lower() == 'copy': for trigger in all_blacklisted: filter_list += "<code>{}</code>\n".format(html.escape(trigger)) else: for trigger in all_blacklisted: filter_list += " - <code>{}</code>\n".format(html.escape(trigger)) split_text = split_message(filter_list) for text in split_text: if text == BASE_BLACKLIST_STRING: msg.reply_text("There are no blacklisted messages here!") return msg.reply_text(text, parse_mode=ParseMode.HTML)
Example #4
Source File: welcome.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def set_welcome(bot: Bot, update: Update) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] msg = update.effective_message # type: Optional[Message] text, data_type, content, buttons = get_welcome_type(msg) if data_type is None: msg.reply_text("You didn't specify what to reply with!") return "" sql.set_custom_welcome(chat.id, content or text, data_type, buttons) msg.reply_text("Successfully set custom welcome message!") return "<b>{}:</b>" \ "\n#SET_WELCOME" \ "\n<b>Admin:</b> {}" \ "\nSet the welcome message.".format(html.escape(chat.title), mention_html(user.id, user.first_name))
Example #5
Source File: __main__.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def migrate_chats(bot: Bot, update: Update): msg = update.effective_message # type: Optional[Message] if msg.migrate_to_chat_id: old_chat = update.effective_chat.id new_chat = msg.migrate_to_chat_id elif msg.migrate_from_chat_id: old_chat = msg.migrate_from_chat_id new_chat = update.effective_chat.id else: return LOGGER.info("Migrating from %s, to %s", str(old_chat), str(new_chat)) for mod in MIGRATEABLE: mod.__migrate__(old_chat, new_chat) LOGGER.info("Successfully migrated!") raise DispatcherHandlerStop
Example #6
Source File: welcome.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def set_goodbye(bot: Bot, update: Update) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] msg = update.effective_message # type: Optional[Message] text, data_type, content, buttons = get_welcome_type(msg) if data_type is None: msg.reply_text("You didn't specify what to reply with!") return "" sql.set_custom_gdbye(chat.id, content or text, data_type, buttons) msg.reply_text("Successfully set custom goodbye message!") return "<b>{}:</b>" \ "\n#SET_GOODBYE" \ "\n<b>Admin:</b> {}" \ "\nSet the goodbye message.".format(html.escape(chat.title), mention_html(user.id, user.first_name))
Example #7
Source File: cust_filters.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def stop_filter(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] args = update.effective_message.text.split(None, 1) if len(args) < 2: return chat_filters = sql.get_chat_triggers(chat.id) if not chat_filters: update.effective_message.reply_text("No filters are active here!") return for keyword in chat_filters: if keyword == args[1]: sql.remove_filter(chat.id, args[1]) update.effective_message.reply_text("Yep, I'll stop replying to that.") raise DispatcherHandlerStop update.effective_message.reply_text("That's not a current filter - run /filters for all active filters.")
Example #8
Source File: cust_filters.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def list_handlers(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] all_handlers = sql.get_chat_triggers(chat.id) if not all_handlers: update.effective_message.reply_text("No filters are active here!") return filter_list = BASIC_FILTER_STRING for keyword in all_handlers: entry = " - {}\n".format(escape_markdown(keyword)) if len(entry) + len(filter_list) > telegram.MAX_MESSAGE_LENGTH: update.effective_message.reply_text(filter_list, parse_mode=telegram.ParseMode.MARKDOWN) filter_list = entry else: filter_list += entry if not filter_list == BASIC_FILTER_STRING: update.effective_message.reply_text(filter_list, parse_mode=telegram.ParseMode.MARKDOWN) # NOT ASYNC BECAUSE DISPATCHER HANDLER RAISED
Example #9
Source File: global_mutes.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def enforce_gmute(bot: Bot, update: Update): # Not using @restrict handler to avoid spamming - just ignore if cant gmute. if sql.does_chat_gmute(update.effective_chat.id) and update.effective_chat.get_member(bot.id).can_restrict_members: user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] msg = update.effective_message # type: Optional[Message] if user and not is_user_admin(chat, user.id): check_and_mute(bot, update, user.id, should_message=True) if msg.new_chat_members: new_members = update.effective_message.new_chat_members for mem in new_members: check_and_mute(bot, update, mem.id, should_message=True) if msg.reply_to_message: user = msg.reply_to_message.from_user # type: Optional[User] if user and not is_user_admin(chat, user.id): check_and_mute(bot, update, user.id, should_message=True)
Example #10
Source File: global_mutes.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def gmutelist(bot: Bot, update: Update): muted_users = sql.get_gmute_list() if not muted_users: update.effective_message.reply_text("There aren't any gmuted users! You're kinder than I expected...") return mutefile = 'Screw these guys.\n' for user in muted_users: mutefile += "[x] {} - {}\n".format(user["name"], user["user_id"]) if user["reason"]: mutefile += "Reason: {}\n".format(user["reason"]) with BytesIO(str.encode(mutefile)) as output: output.name = "gmutelist.txt" update.effective_message.reply_document(document=output, filename="gmutelist.txt", caption="Here is the list of currently gmuted users.")
Example #11
Source File: disable.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def disable(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat # type: Optional[Chat] if len(args) >= 1: disable_cmd = args[0] if disable_cmd.startswith(CMD_STARTERS): disable_cmd = disable_cmd[1:] if disable_cmd in set(DISABLE_CMDS + DISABLE_OTHER): sql.disable_command(chat.id, disable_cmd) update.effective_message.reply_text("Disabled the use of `{}`".format(disable_cmd), parse_mode=ParseMode.MARKDOWN) else: update.effective_message.reply_text("That command can't be disabled") else: update.effective_message.reply_text("What should I disable?")
Example #12
Source File: userinfo.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def about_me(bot: Bot, update: Update, args: List[str]): message = update.effective_message # type: Optional[Message] user_id = extract_user(message, args) if user_id: user = bot.get_chat(user_id) else: user = message.from_user info = sql.get_user_me_info(user.id) if info: update.effective_message.reply_text("*{}*:\n{}".format(user.first_name, escape_markdown(info)), parse_mode=ParseMode.MARKDOWN) elif message.reply_to_message: username = message.reply_to_message.from_user.first_name update.effective_message.reply_text(username + " hasn't set an info message about themselves yet!") else: update.effective_message.reply_text("You haven't set an info message about yourself yet!")
Example #13
Source File: userinfo.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def about_bio(bot: Bot, update: Update, args: List[str]): message = update.effective_message # type: Optional[Message] user_id = extract_user(message, args) if user_id: user = bot.get_chat(user_id) else: user = message.from_user info = sql.get_user_bio(user.id) if info: update.effective_message.reply_text("*{}*:\n{}".format(user.first_name, escape_markdown(info)), parse_mode=ParseMode.MARKDOWN) elif message.reply_to_message: username = user.first_name update.effective_message.reply_text("{} hasn't had a message set about themselves yet!".format(username)) else: update.effective_message.reply_text("You haven't had a bio set about yourself yet!")
Example #14
Source File: rules_bot.py From rules-bot with GNU Affero General Public License v3.0 | 6 votes |
def forward_faq(update: Update, context: CallbackContext): if update.message.chat.username not in [ONTOPIC_USERNAME, OFFTOPIC_USERNAME]: return admins = context.bot.get_chat_administrators(ONTOPIC_USERNAME) if update.effective_user.id not in [x.user.id for x in admins]: return if not update.message: return reply_to = update.message.reply_to_message if not reply_to: return try: update.message.delete() except BadRequest: pass # Forward message to FAQ channel reply_to.forward(const.FAQ_CHANNEL_ID, disable_notification=True)
Example #15
Source File: global_bans.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def gbanlist(bot: Bot, update: Update): banned_users = sql.get_gban_list() if not banned_users: update.effective_message.reply_text("There aren't any gbanned users! You're kinder than I expected...") return banfile = 'Screw these guys.\n' for user in banned_users: banfile += "[x] {} - {}\n".format(user["name"], user["user_id"]) if user["reason"]: banfile += "Reason: {}\n".format(user["reason"]) with BytesIO(str.encode(banfile)) as output: output.name = "gbanlist.txt" update.effective_message.reply_document(document=output, filename="gbanlist.txt", caption="Here is the list of currently gbanned users.")
Example #16
Source File: handlers.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def check_update(self, update): if (isinstance(update, Update) and (update.message or update.edited_message and self.allow_edited)): message = update.message or update.edited_message if message.text and len(message.text) > 1: fst_word = message.text_html.split(None, 1)[0] if len(fst_word) > 1 and any(fst_word.startswith(start) for start in CMD_STARTERS): command = fst_word[1:].split('@') command.append(message.bot.username) # in case the command was sent without a username if self.filters is None: res = True elif isinstance(self.filters, list): res = any(func(message) for func in self.filters) else: res = self.filters(message) return res and (command[0].lower() in self.command and command[1].lower() == message.bot.username.lower()) return False
Example #17
Source File: handlers.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def check_update(self, update): if (isinstance(update, Update) and (update.message or update.edited_message and self.allow_edited)): message = update.message or update.edited_message if sql.is_user_gbanned(update.effective_user.id): return False if message.text and message.text.startswith('/') and len(message.text) > 1: first_word = message.text_html.split(None, 1)[0] if len(first_word) > 1 and first_word.startswith('/'): command = first_word[1:].split('@') command.append(message.bot.username) # in case the command was sent without a username if not (command[0].lower() in self.command and command[1].lower() == message.bot.username.lower()): return False if self.filters is None: res = True elif isinstance(self.filters, list): res = any(func(message) for func in self.filters) else: res = self.filters(message) return res return False
Example #18
Source File: global_bans.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def gbanstat(bot: Bot, update: Update, args: List[str]): if len(args) > 0: if args[0].lower() in ["on", "yes"]: sql.enable_gbans(update.effective_chat.id) update.effective_message.reply_text("I've enabled gbans in this group. This will help protect you " "from spammers, unsavoury characters, and the biggest trolls.") elif args[0].lower() in ["off", "no"]: sql.disable_gbans(update.effective_chat.id) update.effective_message.reply_text("I've disabled gbans in this group. GBans wont affect your users " "anymore. You'll be less protected from any trolls and spammers " "though!") else: update.effective_message.reply_text("Give me some arguments to choose a setting! on/off, yes/no!\n\n" "Your current setting is: {}\n" "When True, any gbans that happen will also happen in your group. " "When False, they won't, leaving you at the possible mercy of " "spammers.".format(sql.does_chat_gban(update.effective_chat.id)))
Example #19
Source File: userinfo.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def set_about_bio(bot: Bot, update: Update): message = update.effective_message # type: Optional[Message] sender = update.effective_user # type: Optional[User] if message.reply_to_message: repl_message = message.reply_to_message user_id = repl_message.from_user.id if user_id == message.from_user.id: message.reply_text("Ha, you can't set your own bio! You're at the mercy of others here...") return elif user_id == bot.id and sender.id not in SUDO_USERS: message.reply_text("Erm... yeah, I only trust sudo users to set my bio.") return text = message.text bio = text.split(None, 1) # use python's maxsplit to only remove the cmd, hence keeping newlines. if len(bio) == 2: if len(bio[1]) < MAX_MESSAGE_LENGTH // 4: sql.set_user_bio(user_id, bio[1]) message.reply_text("Updated {}'s bio!".format(repl_message.from_user.first_name)) else: message.reply_text( "A bio needs to be under {} characters! You tried to set {}.".format( MAX_MESSAGE_LENGTH // 4, len(bio[1]))) else: message.reply_text("Reply to someone's message to set their bio!")
Example #20
Source File: chat_status.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def user_admin(func): @wraps(func) def is_admin(bot: Bot, update: Update, *args, **kwargs): user = update.effective_user # type: Optional[User] if user and is_user_admin(update.effective_chat, user.id): return func(bot, update, *args, **kwargs) elif not user: pass elif DEL_CMDS and " " not in update.effective_message.text: update.effective_message.delete() else: update.effective_message.reply_text("Who dis non-admin telling me what to do?") return is_admin
Example #21
Source File: blacklist.py From SkittBot with GNU General Public License v3.0 | 6 votes |
def add_blacklist(bot: Bot, update: Update): msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] words = msg.text.split(None, 1) if len(words) > 1: text = words[1] to_blacklist = list(set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) for trigger in to_blacklist: sql.add_to_blacklist(chat.id, trigger.lower()) if len(to_blacklist) == 1: msg.reply_text("Added <code>{}</code> to the blacklist!".format(html.escape(to_blacklist[0])), parse_mode=ParseMode.HTML) else: msg.reply_text( "Added <code>{}</code> triggers to the blacklist.".format(len(to_blacklist)), parse_mode=ParseMode.HTML) else: msg.reply_text("Tell me which words you would like to add to the blacklist.")
Example #22
Source File: rules.py From SkittBot with GNU General Public License v3.0 | 5 votes |
def get_rules(bot: Bot, update: Update): chat_id = update.effective_chat.id send_rules(update, chat_id) # Do not async - not from a handler
Example #23
Source File: stickers.py From SkittBot with GNU General Public License v3.0 | 5 votes |
def getsticker(bot: Bot, update: Update): msg = update.effective_message chat_id = update.effective_chat.id if msg.reply_to_message and msg.reply_to_message.sticker: file_id = msg.reply_to_message.sticker.file_id newFile = bot.get_file(file_id) newFile.download('sticker.png') bot.send_document(chat_id, document=open('sticker.png', 'rb')) os.remove("sticker.png") else: update.effective_message.reply_text("Please reply to a sticker for me to upload its PNG.")
Example #24
Source File: chat_status.py From SkittBot with GNU General Public License v3.0 | 5 votes |
def can_restrict(func): @wraps(func) def promote_rights(bot: Bot, update: Update, *args, **kwargs): if update.effective_chat.get_member(bot.id).can_restrict_members: return func(bot, update, *args, **kwargs) else: update.effective_message.reply_text("I can't restrict people here! " "Make sure I'm admin and can appoint new admins.") return promote_rights
Example #25
Source File: chat_status.py From SkittBot with GNU General Public License v3.0 | 5 votes |
def can_pin(func): @wraps(func) def pin_rights(bot: Bot, update: Update, *args, **kwargs): if update.effective_chat.get_member(bot.id).can_pin_messages: return func(bot, update, *args, **kwargs) else: update.effective_message.reply_text("I can't pin messages here! " "Make sure I'm admin and can pin messages.") return pin_rights
Example #26
Source File: chat_status.py From SkittBot with GNU General Public License v3.0 | 5 votes |
def bot_can_delete(func): @wraps(func) def delete_rights(bot: Bot, update: Update, *args, **kwargs): if can_delete(update.effective_chat, bot.id): return func(bot, update, *args, **kwargs) else: update.effective_message.reply_text("I can't delete messages here! " "Make sure I'm admin and can delete other user's messages.") return delete_rights
Example #27
Source File: welcome.py From SkittBot with GNU General Public License v3.0 | 5 votes |
def clean_welcome(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] if not args: clean_pref = sql.get_clean_pref(chat.id) if clean_pref: update.effective_message.reply_text("I should be deleting welcome messages up to two days old.") else: update.effective_message.reply_text("I'm currently not deleting old welcome messages!") return "" if args[0].lower() in ("on", "yes"): sql.set_clean_welcome(str(chat.id), True) update.effective_message.reply_text("I'll try to delete old welcome messages!") return "<b>{}:</b>" \ "\n#CLEAN_WELCOME" \ "\n<b>Admin:</b> {}" \ "\nHas toggled clean welcomes to <code>ON</code>.".format(html.escape(chat.title), mention_html(user.id, user.first_name)) elif args[0].lower() in ("off", "no"): sql.set_clean_welcome(str(chat.id), False) update.effective_message.reply_text("I won't delete old welcome messages.") return "<b>{}:</b>" \ "\n#CLEAN_WELCOME" \ "\n<b>Admin:</b> {}" \ "\nHas toggled clean welcomes to <code>OFF</code>.".format(html.escape(chat.title), mention_html(user.id, user.first_name)) else: # idek what you're writing, say yes or no update.effective_message.reply_text("I understand 'on/yes' or 'off/no' only!") return ""
Example #28
Source File: blacklist.py From SkittBot with GNU General Public License v3.0 | 5 votes |
def unblacklist(bot: Bot, update: Update): msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] words = msg.text.split(None, 1) if len(words) > 1: text = words[1] to_unblacklist = list(set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) successful = 0 for trigger in to_unblacklist: success = sql.rm_from_blacklist(chat.id, trigger.lower()) if success: successful += 1 if len(to_unblacklist) == 1: if successful: msg.reply_text("Removed <code>{}</code> from the blacklist!".format(html.escape(to_unblacklist[0])), parse_mode=ParseMode.HTML) else: msg.reply_text("This isn't a blacklisted trigger...!") elif successful == len(to_unblacklist): msg.reply_text( "Removed <code>{}</code> triggers from the blacklist.".format( successful), parse_mode=ParseMode.HTML) elif not successful: msg.reply_text( "None of these triggers exist, so they weren't removed.".format( successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML) else: msg.reply_text( "Removed <code>{}</code> triggers from the blacklist. {} did not exist, " "so were not removed.".format(successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML) else: msg.reply_text("Tell me which words you would like to remove from the blacklist.")
Example #29
Source File: rules.py From SkittBot with GNU General Public License v3.0 | 5 votes |
def clear_rules(bot: Bot, update: Update): chat_id = update.effective_chat.id sql.set_rules(chat_id, "") update.effective_message.reply_text("Successfully cleared rules!")
Example #30
Source File: rules.py From SkittBot with GNU General Public License v3.0 | 5 votes |
def set_rules(bot: Bot, update: Update): chat_id = update.effective_chat.id msg = update.effective_message # type: Optional[Message] raw_text = msg.text args = raw_text.split(None, 1) # use python's maxsplit to separate cmd and args if len(args) == 2: txt = args[1] offset = len(txt) - len(raw_text) # set correct offset relative to command markdown_rules = markdown_parser(txt, entities=msg.parse_entities(), offset=offset) sql.set_rules(chat_id, markdown_rules) update.effective_message.reply_text("Successfully set rules for this group.")