def sendMessage(self, chat_id, message, **kwargs): return_value = None while True: try: Log.debug( 2, 'Calling telepot_bot.sendMessage() [chat_id = {},message = {}, kwargs = {}]', chat_id, repr(message), kwargs) return_value = self.bot.sendMessage(chat_id, message, **kwargs) break except telepot.exception.TelegramError as e: # Markdown fail if self.fail_with_md is not None and e[-1]['error_code'] == 400 and \ 'Can\'t find end of the entity starting at byte' in e[-1]['description']: # Must fail safe return_value = self.bot.sendMessage( chat_id, 'Error: {}\nRaw description: {}'.format( self.fail_with_md, e[-1]['description'])) break else: Log.error('Raise exception: {}', repr(e)) raise e except Exception as e: Log.error('Exception {} occurred', e.__name__) Log.debug( 1, 'on telepot_bot.sendMessage() [chat_id = {},message = {}, kwargs = {}]', chat_id, repr(message), kwargs) time.sleep(self.WAIT_TIME) return return_value
def main(): Log.info('Strat initializing....') Log.info('Debug enable: {}', Log.get_debug_info()[0]) Log.debug(1, 'Debug level: {}', Log.get_debug_info()[1]) bot_class() Log.info('Bot is now running!') while True: time.sleep(30)
def sql_clear_comment(sql_file_path): with open(sql_file_path) as fin: r = fin.readlines() with open(sql_file_path,'w') as fout: for x in r: if x[:2] != '--': #content.append(x) fout.write(x) Log.debug(5, '[x = {}]',x[:-1])
def load(self): Log.debug(2, 'Entering group_cache_class.load()') self.__init__() with MainDatabase() as db: result = db.query("SELECT * FROM `welcomemsg`") for x in result: self.add(x) #if init: #syncLock.release() #kwargs['syncLock'].release() Log.info('Load welcomemsg table successful.') Log.debug(2, 'Exiting group_cache_class.load()')
def main(): Log.info('Strat initializing....') Log.info('Debug enable: {}', Log.get_debug_info()[0]) Log.debug(1, 'Debug level: {}', Log.get_debug_info()[1]) bot_class() Log.info('Bot is now running!') if Config.git.switch: Log.info('Starting BackupSQL daemon') BackupSQL.sql_backup_daemon().start() Log.info('BackupSQL daemon is now running') while True: time.sleep(30)
def glance(self, msg): while True: try: Log.debug(2, 'Calling telepot.glance()') content_type, chat_type, chat_id = telepot.glance(msg) Log.debug( 2, 'Exiting telepot.glance() [content_type = {}, chat_type = {}, chat_id = {}]', content_type, chat_type, chat_id) break except telepot.exception.TelegramError as e: Log.error('Raise exception: {}', e.__name__) raise e except Exception as e: Log.error('Exception {} occurred', e.__name__) time.sleep(0.03) return (content_type, chat_type, chat_id)
def get(self, chat_id): try: Log.debug( 2, 'Calling group_cache_class.get() [chat_id in self.g = {}]', chat_id in self.g) Log.debug(2, '[self.g[{}] = {}]', chat_id, self.g[chat_id]) return self.g[chat_id] except KeyError: Log.error('Can\'t find {} in get()', chat_id) self.add((chat_id, None, 0, 1, 0, 0, b64encode(repr([]))), not_found=True) self.__db_add(chat_id) self.bot.sendMessage( chat_id, 'It\'s seems that database broken, please reset welcome message.' ) return {'msg': None}
def split_list(l, split_count, Fill_with_None=False): assert isinstance(l, list) or l is None Count = len(l)//split_count Log.debug(3, 'Count:{}', Count) if Count == 0: tmp = [[x] for x in l] if Fill_with_None: for x in xrange(len(tmp), split_count): tmp.append(None) return tmp if len(l)%split_count: Count += 1 tmp = [l[Count*x:Count*(x+1)] for x in xrange(0, split_count)] for x in tmp: if x == []: tmp.remove(x) if Fill_with_None: for x in xrange(len(tmp), split_count): tmp.append(None) return tmp
def __init__(self, *args, **kwargs): Log.debug(2, 'Enter telepot_bot.__init__()') WAIT_TIME = 0.03 Log.debug(2, '[bot_token = {}]', Config.bot.bot_token) Log.info('Initializing bot settings...') self.bot = TelepotBotBase.Bot(Config.bot.bot_token) self.bot_id = int( Config.bot.bot_token[:Config.bot.bot_token.find(':')]) Log.info('Success login telegram bot with Token {}**************', Config.bot.bot_token[:Config.bot.bot_token.find(':') + 5]) Log.info('Loading telepot_bot.custom_init()') self.fail_with_md = None self.custom_init(*args, **kwargs) Log.info('Loading telepot_bot.custom_init() successful') Log.info('Bot settings initialized successful!') Log.debug(2, 'Exit telepot_bot.__init__()')
def getid(self): Log.debug(2, 'Calling telepot_bot.getid() [return {}]', self.bot_id) return self.bot_id
def delete(self, user_id): with self.WriteLock: self.members.pop(user_id) self.writeFile() Log.debug(2 ,'{}: Delete {} from database successful', self.usage_str, user_id)
def write(self, user_id, user_name): Log.debug(2, 'user_id is {}', user_id) with self.WriteLock: self.members[user_id] = user_name self.writeFile() Log.debug(2 ,'{}: Write {} to database successful', self.usage_str, user_id)