def error(update, context): """Log the error and send a telegram message to notify the developer.""" # Log the error before we do anything else, so we can see it even if something breaks. telelogger.error(msg="Exception while handling an update:", exc_info=context.error) # traceback.format_exception returns the usual python message about an exception, but as a # list of strings rather than a single string, so we have to join them together. tb_list = traceback.format_exception(None, context.error, context.error.__traceback__) tb = ''.join(tb_list) # Build the message with some markup and additional information about what happened. # You might need to add some logic to deal with messages longer than the 4096 character limit. message = ( '<b>An exception was raised while handling an update</b>\n' '<pre>{}</pre>' ).format( html.escape(tb) ) # Finally, send the message string = str(config.get('DEVS')).replace('[', '') string = string.replace(']', '') string = string.replace(' ', '') devs = list(string.split(',')) for dev in devs: context.bot.send_message(chat_id=dev, text=message, parse_mode=ParseMode.HTML)
def get_session(self): # Localhost session: str = config.get(f'instasession:{self.user_id}') if not session: return None self.username = str(session) return str(session)
def save_creds(self): """ Store working instagram credentials (username and password) """ creds = config.get('instacreds:{}'.format(self.user_id)) # Localhost if not creds: creds = dict() creds[self.username] = self.password config.set('instacreds:{}'.format(self.user_id), creds)
def get_all_creds(self): creds = config.get('instacreds:{}'.format(self.user_id)) if not creds: return None hascreds = False for cred in creds: if creds.get(cred): hascreds = True break if not hascreds: return None return creds
def get_creds(self): session = self.get_session() creds = config.get('instacreds:{}'.format(self.user_id)) if not creds: return False else: self.set_username( session if isinstance(session, str) else list(creds.keys())[0]) if creds.get(self.username): self.set_password(creds.get(self.username)) return True else: return False
def insta_error_callback(driver): driver.save_screenshot('error.png') from ozanbot import telegram_bot as bot, config # TODO users_str = config.get('DEVS') if isinstance(users_str, str): users_str = users_str.replace('[', '') users_str = users_str.replace(']', '') users_str = users_str.replace(' ', '') users = users_str.split(',') for index, user in enumerate(users): users[index] = int(user) else: users = users_str for dev in users: bot.send_photo(chat_id=dev, photo=open('{}.png'.format('error'), 'rb'), caption='There was an error with the bot. Check logs')
def report_error(self, error=None, send_screenshot=False, screenshot_name=''): string = str(config.get('DEVS')).replace('[', '') string = string.replace(']', '') string = string.replace(' ', '') devs = list(string.split(',')) for dev in devs: if send_screenshot: self.send_photo( chat_id=int(dev), photo=open('{}.png'.format(screenshot_name), 'rb'), caption='There was an error with the FFInstaBot: \n{}'. format(error)) else: self.send_message( chat_id=int(dev), text='There was an error with the FFInstaBot: \n{}'.format( error))
def check_auth(update, context): users_str = config.get('USERS') if isinstance(users_str, str): users_str = users_str.replace('[', '') users_str = users_str.replace(']', '') users_str = users_str.replace(' ', '') users = users_str.split(',') for index, user in enumerate(users): users[index] = int(user) else: users = users_str if update.effective_user.id in users: telelogger.debug('User is authorized to use the bot') return True else: telelogger.debug('User is NOT authorized to use the bot.') try: send_message(update, context, not_authorized_text) return False except Exception as error: telelogger.debug('Error in sending message: {}'.format(error)) return False
def delete_creds(self): session = self.get_session() self.username = None self.password = None creds = config.get(f'instacreds:{self.user_id}') try: del creds[session] except: pass config.set('instacreds:{}'.format(self.user_id), creds) newsession = None for key in list(creds.keys()): if key != session: newsession = key self.set_session(key) self.set_username(key) break applogger.debug(f'Set session: {newsession}') if not newsession: self.set_session(None)
def instaclient_error_callback(driver): from ozanbot import telegram_bot as bot driver.save_screenshot('error.png') bot.report_error('instaclient.__find_element() error.', send_screenshot=True, screenshot_name='error') os.remove('error.png') LOCALHOST = True queue = None if os.environ.get('PORT') not in (None, ""): # Code running locally LOCALHOST = False # Initialize Bot from ozanbot.config import config BOT_TOKEN = config.get('BOT_TOKEN') URL = config.get('SERVER_APP_DOMAIN') PORT = int(os.environ.get('PORT', 5000)) request = Request(con_pool_size=8) defaults = Defaults(parse_mode=ParseMode.HTML, run_async=True) q = mq.MessageQueue(all_burst_limit=3, all_time_limit_ms=3000) telegram_bot = MQBot(BOT_TOKEN, request=request, mqueue=q, defaults=defaults) updater = Updater(bot=telegram_bot, use_context=True) # SET UP BOT COMMAND HANDLERS applogger.debug(f'Initiate setup') from ozanbot.bot import setup setup.setup(updater)