def feed_url(update, url, **chat_info): arg_url = FeedHandler.format_url_string(string=url) chat_id = update.chat.id # _check if argument matches url format if not FeedHandler.is_parsable(url=arg_url): text = "Sorry! It seems like '" + \ str(arg_url) + "' doesn't provide an RSS news feed.. Have you tried another URL from that provider?" envia_texto(bot=bot, chat_id=chat_id, text=text) return chat_id = chat_info['chat_id'] chat_name = chat_info.get('chat_name') user_id = update.from_user.id result = db.set_url_to_chat(chat_id=str(chat_id), chat_name=str(chat_name), url=url, user_id=str(user_id)) if result: text = "I successfully added " + arg_url + " to your subscriptions!" else: text = "Sorry, " + update.from_user.first_name + \ "! I already have that url with stored in your subscriptions." envia_texto(bot=bot, chat_id=chat_id, text=text)
def get_chat_by_username(update, user_name=None): get_chat = None try: if user_name: user_name = user_name if user_name[0] == '@' else '@' + str( user_name) chat_id = update.chat.id if user_name == '@this' or not user_name else user_name get_chat = bot.get_chat(chat_id=chat_id) except telebot.apihelper.ApiException as _: if user_name: text = f'I cant resolved username {user_name}' envia_texto(bot=bot, chat_id=update.chat.id, text=text, parse_mode='HTML') logger.warning(f"{e}") user = {} if get_chat: user.update({'id': str(get_chat.id) if get_chat.id else None}) user.update({'type': str(get_chat.type)}) if get_chat.type else None user.update({'title': get_chat.title}) if get_chat.title else None user.update({'username': '******' + get_chat.username}) if get_chat.username else None user.update({ 'first_name': get_chat.first_name if get_chat.first_name else None }) user.update( {'last_name': get_chat.last_name if get_chat.last_name else None}) user.update({ 'description': get_chat.description if get_chat.description else None }) return user if get_chat else None
def _welcome(update, member=None): """ Welcomes a user to the chat """ chat_id = update.chat.id chat_title = update.chat.title first_name = member.first_name logger.info( f'{escape(first_name)} joined to chat {chat_id} ({escape(chat_title)})' ) # Pull the custom message for this chat from the database text_group = db.get_value_name_key('group:' + str(chat_id), 'chat_welcome') if not text_group: return # Use default message if there's no custom one set welcome_text = f'Hello $username! Welcome to $title {emojize(":grinning_face:")}' if text_group: text = welcome_text + '\n' + text_group # Replace placeholders and send message else: text = welcome_text # Replace placeholders and send message text = text.replace('$username', first_name).replace('$title', chat_title) envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML')
def _introduce(update): """ Introduces the bot to a chat its been added to and saves the user id of the user who invited us. """ me = bot.get_me() if me.username == '@' + BOT_NAME_LD: _set_daily_liturgy(update) return chat_title = update.chat.title chat_id = update.chat.id first_name = update.from_user.first_name chat_name = ''.join('@' if update.chat.username or update.from_user. username else update.from_user.first_name) user_id = update.from_user.id logger.info( f'Invited by {user_id} to chat {chat_id} ({escape(chat_title)})') db.update_group(chat_id=chat_id, chat_name=chat_name, chat_title=chat_title, user_id=user_id) text = f'Hello {escape(first_name)}! I will now greet anyone who joins this chat ({chat_title}) with a' \ f' nice message {emojize(":grinning_face:")} \n\ncheck the /help command for more info!' envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML')
def command_control(update, command): """ Disables the goodbye message """ chat_id = update.chat.id # _check admin privilege and group context if _check(update): if command == 'disable_welcome': commit = db.set_name_key('group:' + str(chat_id), {'chat_welcome': 'False'}) elif command == 'disable_goodbye': commit = db.set_name_key('group:' + str(chat_id), {'chat_goodbye': 'False'}) elif command == 'lock': commit = db.set_name_key('group:' + str(chat_id), {'chat_lock': 'True'}) elif command == 'unlock': commit = db.set_name_key('group:' + str(chat_id), {'chat_lock': 'False'}) elif command == 'quiet': commit = db.set_name_key('group:' + str(chat_id), {'chat_quiet': 'True'}) elif command == 'unquiet': commit = db.set_name_key('group:' + str(chat_id), {'chat_quiet': 'False'}) else: commit = False if commit: envia_texto(bot=bot, chat_id=chat_id, text='Got it!', parse_mode='HTML')
def _check(update, override_lock=None): """ Perform some hecks on the update. If checks were successful, returns True, else sends an error message to the chat and returns False. """ chat_id = update.chat.id user_id = update.from_user.id if chat_id > 0: text = 'Please add me to a group first!' envia_texto(bot=bot, chat_id=chat_id, text=text) return False locked = override_lock if override_lock is not None \ else bool(db.get_value_name_key('group:' + str(chat_id), 'chat_lock')) if locked and int( db.get_value_name_key('group:' + str(chat_id), 'chat_adm')) != user_id: if not bool( db.get_value_name_key('group:' + str(chat_id), 'chat_quiet')): text = 'Sorry, only the person who invited me can do that.' envia_texto(bot=bot, chat_id=chat_id, text=text) return False return True
def get_key(update): args = update.text.strip()[update.entities[0].length + 1:].split(' ') if update.entities else None chat_id = update.chat.id if len(args) == 1: keys = db.find_names(args[0]) for k in keys: text = str('<code>/removekey ' + str(k) + '</code>') envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML')
def remove_key(update): args = update.text.strip()[update.entities[0].length + 1:].split(' ') if update.entities else None chat_id = update.chat.id text = 'I removed ' if len(args) == 1: key = args[0] if db.redis.delete(args[0]) == 1: text = text + key envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML')
def add_url(update): """ Adds a rss subscription to user """ args = update.text.strip()[update.entities[0].length + 1:].split(' ') if update.entities else None chat_id = update.chat.id user_id = update.from_user.id # _check admin privilege and group context if chat_id < 0: if not _check(update): return text = "Sorry! I could not add the entry! " \ "Please use the the command passing the following arguments:\n\n " \ "<code>/addurl url</code> or \n <code>/addurl username url</code> \n\n Here is a short example: \n\n " \ "/addurl http://www.feedforall.com/sample.xml \n\n" \ "/addurl @username http://www.feedforall.com/sample.xml " if len(args) > 2 or not args or args[0] == '': envia_texto(bot=bot, chat_id=user_id, text=text, parse_mode='HTML') return elif len(args) == 2: chat_name = args[0] url = args[1] chat_info = get_chat_by_username(update, chat_name) text = "I don't have access to chat " + chat_name + '\n' + text if chat_info is None: envia_texto(bot=bot, chat_id=user_id, text=text, parse_mode='HTML') else: chat_info = { 'chat_id': chat_info['id'], 'chat_name': chat_info['username'] } feed_url(update, url, **chat_info) else: url = args[0] user_name = '@' + update.chat.username if update.chat.username else None first_name = update.from_user.first_name if update.from_user.first_name else None chat_title = update.chat.title if update.chat.title else None chat_name = user_name or chat_title or first_name chat_info = { 'chat_id': chat_id, 'chat_name': chat_name, 'user_id': user_id } feed_url(update, url, **chat_info)
def stop(update): """ Stops the bot from working """ chat_id = update.chat.id # _check admin privilege and group context if chat_id < 0: if not _check(update): return text = "Oh.. Okay, I will not send you any more news updates! " \ "If you change your mind and you want to receive messages " \ "from me again use /start command again!" envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML')
def _set_daily_liturgy(update): chat_id = update.chat.id chat_name = '@' + update.chat.username or '@' + update.from_user.username \ or update.from_user.first_name chat_title = update.chat.title or update.from_user.first_name user_id = update.from_user.id url = 'http://feeds.feedburner.com/evangelhoddia/dia' text = 'You will receive the daily liturgy every day.\nFor more commands click /help' db.set_url_to_chat(chat_id=chat_id, chat_name=chat_name, url=url, user_id=user_id) envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML') logger.info( f'Invited by {user_id} to chat {chat_id} ({escape(chat_title)})')
def start(update): """ Prints help text """ me = bot.get_me() if me.username == 'SejaBemVindo_bot': _set_daily_liturgy(update) return chat_id = update.chat.id from_user = update.from_user.id if not bool(db.get_value_name_key('group:' + str(chat_id), 'chat_quiet')) \ or str(db.get_value_name_key('group:' + str(chat_id), 'chat_adm')) == str(from_user): envia_texto(bot=bot, chat_id=chat_id, text=help_text, parse_mode='MARKDOWN', disable_web_page_preview=True)
def get_user_info(update): chat_id = update.chat.id args = update.text.strip()[update.entities[0].length + 1:].split(' ')[0] if update.entities else None command = update.text[1:update.entities[0].length] or None if args != '': get_chat = get_chat_by_username(update, user_name=args) or None else: get_chat = get_chat_by_username(update) if get_chat: get_chat['id'] = f"<code>{get_chat['id']} </code>" text = '\n'.join(f'{k}: {v}' for k, v in get_chat.items()) if text and command == 'me': envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML')
def remove_url(update): """ Removes an rss subscription from user """ args = update.text.strip()[update.entities[0].length + 1:].split(' ') if update.entities else None chat_id = update.chat.id text = "Sorry! I could not remove the entry! " \ "Please use the the command passing the following arguments:\n\n " \ "<code>/removeurl url</code> or \n <code>/removeurl username url</code> \n\n " \ "Here is a short example: \n\n " \ "/removeurl http://www.feedforall.com/sample.xml \n\n" \ "/removeurl @username http://www.feedforall.com/sample.xml " if len(args) > 2 or not args or args == '': envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML') return user_id = update.from_user.id chat_name = args[0] if len(args) == 2 else update.from_user.username if update.from_user.username else \ update.from_user.first_name logger.error(f'remove_url {str(user_id)} {chat_name}') chat_id_db = db.get_chat_id_for_chat_name( user_id, chat_name) if chat_name else update.chat.id url = args[1] if len(args) == 2 else args[0] if chat_id_db is None: text = "Don't exist chat " + chat_name + '\n' + text envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML') else: exist_url = db.exist_url_to_chat(user_id, chat_id, url) if not exist_url: chat_name = chat_name or update.from_user.first_name text = "Don't exist " + url + " for chat " + chat_name + '\n' + text envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML') result = None else: result = True if db.del_url_for_chat(chat_id, url) else None if result: text = "I removed " + url + " from your subscriptions!" else: text = "I can not find an entry with label " + \ url + " in your subscriptions! Please check your subscriptions using " \ "/listurl and use the delete command again!" envia_texto(bot=bot, chat_id=chat_id, text=text) names_url = db.find_names(url) if len(names_url) == 1: db.del_names(names_url)
def set_welcome(u): """ Sets custom welcome message """ args = u.text.strip()[u.entities[0].length + 1:] if u.entities else None chat_id = u.chat.id # _check admin privilege and group context if not _check(u): return # Split message into words and remove mentions of the bot # set_text = r' '.join(args) # Only continue if there's a message if not args: text = 'You need to send a message, too! For example:\n' \ '<code>/welcome The objective of this group is to...</code>' envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML') return # Put message into database db.set_name_key('group:' + str(chat_id), {'chat_welcome': args}) envia_texto(bot=bot, chat_id=chat_id, text='Got it!', parse_mode='HTML')
def all_url(update): """ Displays a list of all user subscriptions """ chat_id = update.chat.id # _check admin privilege and group context if chat_id < 0: if not _check(update): return text = "Here is a list of all subscriptions I stored for you!" envia_texto(bot=bot, chat_id=chat_id, text=text) urls = db.get_urls_activated() for url in urls: last_update = db.get_update_url(url) text = 'last_update: ' + last_update['last_update'] + '\n\n' \ + 'last_url: <code>' + last_update['last_url'] + '</code>\n\n' \ + 'url: <code>' + last_update['url'] + '</code>' envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML')
def list_url(update): """ Displays a list of all user subscriptions """ user_id = update.from_user.id chat_id = update.chat.id # _check admin privilege and group context if chat_id < 0: if not _check(update): return text = "Here is a list of all subscriptions I stored for you!" envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML') urls = db.get_chat_urls(user_id=user_id) for url in urls: url = (str(url['chat_name']) + ' ' if url['chat_name'] and int(url['chat_id']) < 0 else '') + url['url'] text = '<code>/removeurl ' + url + '</code>' # update.reply_text(message) envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML')
def goodbye(update): """ Sends goodbye message when a user left the chat """ chat_id = update.chat.id chat_title = update.chat.title first_name = update.left_chat_member.first_name logger.info( f'{escape(first_name)} left chat {chat_id} ({escape(chat_title)})') # Pull the custom message for this chat from the database text = db.get_value_name_key('group:' + str(chat_id), 'chat_goodbye') # Goodbye was disabled if text == 'False': return # Use default message if there's no custom one set if text is None: text = 'Goodbye, $username!' # Replace placeholders and send message text = text.replace('$username', first_name).replace('$title', chat_title) envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML')
def set_goodbye(update): """ Enables and sets custom goodbye message """ args = update.text.strip()[update.entities[0].length + 1:] if update.entities else None chat_id = update.chat.id # _check admin privilege and group context if not _check(update): return # Split message into words and remove mentions of the bot # set_text = ' '.join(args) # Only continue if there's a message if not args: text = 'You need to send a message, too! For example:\n' \ '<code>/goodbye Goodbye, $username!</code>' envia_texto(bot=bot, chat_id=chat_id, text=text, parse_mode='HTML') return # Put message into database db.set_name_key('group:' + str(chat_id), {'chat_goodbye': args}) envia_texto(bot=bot, chat_id=chat_id, text='Got it!', parse_mode='HTML')
def send_newest_messages(self, message, url): if not self._finished.isSet(): names_url = self.db.get_names_for_user_activated(url) update_url = False for name in names_url: chat_id = int(self.db.get_value_name_key(name, 'chat_id')) if chat_id: result = envia_texto(bot=self.bot, chat_id=chat_id, text=message, parse_mode='html') if not result: self.errors(chat_id=chat_id, url=url) else: update_url = True return update_url