def test_track(self): """Test sending event to botan""" print('Test sending event to botan') botan = Botan(self.token) message = MessageMock(self._chat_id) result = botan.track(message, 'named event') self.assertTrue(result)
def test_track(self): """Test sending event to botan""" print('Test sending event to botan') botan = Botan(self.token) message = MessageMock(self._chat_id) result = botan.track(message, 'named event') self.assertTrue(result)
def test_wrong_endpoint(self): """Test wrong endpoint""" print('Test wrong endpoint') botan = Botan(self.token) botan.url_template = 'https://api.botaaaaan.io/traccc?token={token}&uid={uid}&name={name}' message = MessageMock(self._chat_id) result = botan.track(message, 'named event') self.assertFalse(result)
def test_wrong_message(self): """Test sending wrong message""" print('Test sending wrong message') botan = Botan(self.token) message = MessageMock(self._chat_id) message = delattr(message, 'chat_id') result = botan.track(message, 'named event') self.assertFalse(result)
def test_track_fail(self): """Test fail when sending event to botan""" print('Test fail when sending event to botan') botan = Botan(self.token) botan.url_template = 'https://api.botan.io/traccc?token={token}&uid={uid}&name={name}' message = MessageMock(self._chat_id) result = botan.track(message, 'named event') self.assertFalse(result)
def test_wrong_endpoint(self): """Test wrong endpoint""" print('Test wrong endpoint') botan = Botan(self.token) botan.url_template = 'https://api.botaaaaan.io/traccc?token={token}&uid={uid}&name={name}' message = MessageMock(self._chat_id) result = botan.track(message, 'named event') self.assertFalse(result)
def test_wrong_message(self): """Test sending wrong message""" print('Test sending wrong message') botan = Botan(self.token) message = MessageMock(self._chat_id) message = delattr(message, 'chat_id') result = botan.track(message, 'named event') self.assertFalse(result)
def test_track_fail(self): """Test fail when sending event to botan""" print('Test fail when sending event to botan') botan = Botan(self.token) botan.url_template = 'https://api.botan.io/traccc?token={token}&uid={uid}&name={name}' message = MessageMock(self._chat_id) result = botan.track(message, 'named event') self.assertFalse(result)
def test_wrong_message(self): botan = Botan(self.token) message = MessageMock(self._chat_id) message = delattr(message, 'chat_id') result = botan.track(message, 'named event') self.assertFalse(result)
def test_track(self): botan = Botan(self.token) message = MessageMock(self._chat_id) result = botan.track(message, 'named event') self.assertTrue(result)
def test_wrong_message(self): botan = Botan(self.token) message = MessageMock(self._chat_id) message = delattr(message, 'chat_id') result = botan.track(message, 'named event') self.assertFalse(result)
def test_track(self): botan = Botan(self.token) message = MessageMock(self._chat_id) result = botan.track(message, 'named event') self.assertTrue(result)
class CakesBot: __help = '''Напишите боту слово, что бы получить случайный пирожок с ним. Доступные команды: /last - присылает пять последних пирожков /random - присылает вам случайный пирожок /help - выводит эту справку /about - выводит информацию о боте Для поиска напишите в чате имя бота и ключевые слова. Пример: @pirozkibot олег ''' __about = ''' Загружено %s пирожка Написано just for fun. Контент берётся отсюда: https://vk.com/perawki Вы можете проголосовать за бота по ссылке: https://telegram.me/storebot?start=pirozkibot Автор бота: @HissingSound ''' def __init__(self, updater, database, botan_token=''): self.updater = updater if botan_token: self.botan = Botan(botan_token) self.__db = database def start(self, bot, update): self.__message_info(update.message, 'start') bot.sendMessage(update.message.chat_id, text=self.__help) def help(self, bot, update): self.__message_info(update.message, 'help') bot.sendMessage(update.message.chat_id, text=self.__help) def random(self, bot, update, args): self.__message_info(update.message, 'random') poem = '' if args: word = ' '.join(args) poem = self.__db.randomByWord(word) else: poem = self.__db.random() bot.sendMessage(update.message.chat_id, text=poem) def last(self, bot, update): self.__message_info(update.message, 'last') poems = self.__db.last(5) bot.sendMessage(update.message.chat_id, text=''.join(poems)) def about(self, bot, update, args): self.__message_info(update.message, 'about') bot.sendMessage(update.message.chat_id, text=self.__about % self.__db.count()) def inline_search(self, bot, update): if update.inline_query: user = update.inline_query.from_user query = update.inline_query.query results = list() if query: logger.info('Inline: %s from %s @%s %s' % (query, user.first_name, user.username, user.last_name)) poems = self.__db.listByWord(query) if poems: for poem in poems: text, author = poem['text'], poem['author'] msg = '\n'.join([text, author]) uniqueId = hex(getrandbits(64))[2:] article = InlineQueryResultArticle(id=uniqueId, title=author, message_text=msg, description=text) results.append(article) bot.answerInlineQuery(update.inline_query.id, results) def error(self, bot, update, error): self.__message_info(update.message, 'error') logger.warn('Update "%s" caused error "%s"' % (update, error)) def unknow_command(self, bot, update, *args): self.__message_info(update.message) def message(self, bot, update): self.__message_info(update.message, 'message') poem = self.__db.randomByWord(update.message.text) bot.sendMessage(update.message.chat_id, text=poem) def __message_info(self, message, command='unknow'): if self.botan: self.botan.track( message=message, event_name=command ) user = message.from_user logger.info(u'%s from %s @%s %s' % (message.text, user.first_name, user.username, user.last_name)) def cli_unknow_command(self, bot, update): logger.info('Unknow command') def signal_handler(self, signum, frame): self.is_idle = False self.updater.stop() def idle(self, stop_signals=(SIGINT, SIGTERM, SIGABRT)): self.is_idle = True for sig in stop_signals: signal(sig, self.signal_handler) self.update_queue = self.updater.start_polling(poll_interval=0.1, timeout=10) while self.is_idle: try: text = raw_input() except NameError: text = input() if text == 'stop': self.updater.stop() break elif len(text) > 0: self.update_queue.put(text)
class GitTrendsBot: __about_text = ( 'Shows GitHub trending repositories from: http://github.com/trending\n' 'Written \'Just for Fun\' by [ShelomentsevD](https://github.com/shelomentsevd)\n' '[Source code here](https://github.com/shelomentsevd/GitTrendsBot)' ) __help_text = ( 'Supports commands:\n' '/today [language] - shows today trending repositories written in [language]\n' '/week [language] - same as /today\n' '/month [language] - same as previous two\n' '/help - this text\n' '/about - bot information\n' ) def __init__(self, telegram, botan=None): if botan: from telegram.utils.botan import Botan self.__botan = Botan(botan) self.__updater = Updater(telegram) dp = self.__updater.dispatcher dp.addTelegramCommandHandler('start', self.__start) dp.addTelegramCommandHandler('help', self.__help) dp.addTelegramCommandHandler('about', self.__about) dp.addTelegramCommandHandler('today', self.__today) dp.addTelegramCommandHandler('week', self.__week) dp.addTelegramCommandHandler('month', self.__month) dp.addUnknownTelegramCommandHandler(self.__unknow) dp.addErrorHandler(self.__error) def __logger_wrap(self, message, command): if self.__botan: self.__botan.track( message=message, event_name=command ) user = message.from_user logger.info(u'%s from %s @%s %s' % (message.text, user.first_name, user.username, user.last_name)) def __trends_wrap(self, period, language=''): trends = list() description = ( '*Name*: [%s](http://github.com/%s)\n' '*Description*: %s\n' '*Language*: %s\n' '%s') try: trends = git.get_trends(period, language) except Exception as e: logger.exception(e) repos = [description % (repo['name'], repo['name'], repo['description'], repo['language'], re.sub(u'stars', Emoji.WHITE_MEDIUM_STAR.decode('utf-8'), repo['stars'])) for repo in trends] return '\n\n'.join(repos) def __start(self, bot, update, args): self.__logger_wrap(update.message, 'start') bot.sendMessage(update.message.chat_id, text=self.__help_text) def __help(self, bot, update): self.__logger_wrap(update.message, 'help') bot.sendMessage(update.message.chat_id, text=self.__help_text) def __about(self, bot, update): self.__logger_wrap(update.message, 'about') bot.sendMessage(update.message.chat_id, text=self.__about_text, parse_mode=ParseMode.MARKDOWN) def __today(self, bot, update, args): self.__logger_wrap(update.message, 'today') result = '' if args: result = self.__trends_wrap('daily', args[0]) else: result = self.__trends_wrap('daily') bot.sendMessage(update.message.chat_id, text=result, parse_mode=ParseMode.MARKDOWN) def __week(self, bot, update, args): self.__logger_wrap(update.message, 'week') result = '' if args: result = self.__trends_wrap('weekly', args[0]) else: result = self.__trends_wrap('weekly') bot.sendMessage(update.message.chat_id, text=result, parse_mode=ParseMode.MARKDOWN) def __month(self, bot, update, args): self.__logger_wrap(update.message, 'month') result = '' if args: result = self.__trends_wrap('monthly', args[0]) else: result = self.__trends_wrap('monthly') bot.sendMessage(update.message.chat_id, text=result, parse_mode=ParseMode.MARKDOWN) def __unknow(self, bot, update): self.__logger_wrap(update.message, 'unknow') def __error(self, bot, update, error): self.__logger_wrap(update.message, 'error') logger.warn('Update "%s" caused error "%s"' % (update, error)) def idle(self): self.__updater.start_polling() self.__updater.idle()