def post(self): data = request.get_json() sample_schema = SampleSchema() try: validated_data = sample_schema.load(data) except ValidationError as err: return response_message( 'fail', 400, 'Validation Error', err.messages ) # run a background service from scheduler import sched, say_hi sched.add_job( say_hi ) return response_message( 'success', 200, 'Successfully hit this endpoint', validated_data )
def my_tacos_callback(bot, message): """ shows users taco-balance """ cid = get_cid(message) uid = str(get_uid(message)) with db: chat = Chats.get(Chats.cid == message.chat.id) tacos = Tacos.get(Tacos.chat == cid) balances = tacos.taco_balance delete_message(bot, message) user_name = store_name(message.from_user) if uid in balances.keys(): balance = balances.get(uid) else: balances.update({uid: default_taco_amount}) tacos.taco_balance = balances tacos.save() balance = default_taco_amount if chat.less is True: comment = '' else: if balance < 25: comment = balance_comment_low elif balance > 60: comment = balance_comment_high else: comment = balance_comment_medium if chat.autohide: msg = bot.send_message(chat_id=cid, text=balance_phrase.format(user_name, balance, comment), reply_to_message_id=get_mid(message), parse_mode='html') time = datetime.datetime.now() + datetime.timedelta(minutes=chat.autohide_delay) sched.add_job(delete_message, 'date', run_date=time, args=[bot, msg]) else: ok_button = InlineKeyboardButton('OK', callback_data='delete:{}'.format(message.from_user.id)) ok_keyboard = InlineKeyboardMarkup([[ok_button]]) msg = bot.send_message(chat_id=cid, text=balance_phrase.format(user_name, balance, comment), reply_to_message_id=get_mid(message), reply_markup=ok_keyboard, parse_mode='html') chat.mids = [msg.message_id] chat.save()
def schedule_crawler_detail_jobs(): # every 8 hour sched.add_job(fill_missing_info, 'interval', hours=8) # every 30 minutes sched.add_job(crawl_detail, 'interval', minutes=30) # run now sched.add_job(crawl_detail, 'date')
api_id = config('API_ID', cast=int) api_hash = config('API_HASH') bot_username = ensure_no_at_sign(config('BOT_USERNAME', default='HeyTacoBot')) logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.getLevelName(config('LOG_LEVEL', default='INFO'))) bot = Client(session_name=bot_username, api_id=api_id, api_hash=api_hash, bot_token=bot_token) if __name__ == '__main__': sched.add_job(tacoinflator, trigger='cron', hour='0,12') bot.add_handler(new_chat_handler, group=-1) bot.add_handler(store_names_handler, group=-1) bot.add_handler(start_handler) bot.add_handler(help_handler) bot.add_handler(self_kick_handler) bot.add_handler(chat_reply_handler) bot.add_handler(my_tacos_handler) bot.add_handler(taco_top_handler) bot.add_handler(less_handler) bot.add_handler(taco_mention_handler) bot.add_handler(delete_handler) bot.add_handler(autohide_delay_handler) bot.add_handler(autohide_handler)
def give_tacos(bot, message, sender, receiver): cid = get_cid(message) tacos = Tacos.get(Tacos.chat == cid) chat = Chats.get(Chats.cid == message.chat.id) ok_button = InlineKeyboardButton('OK', callback_data='delete:{}'.format( message.from_user.id)) ok_keyboard = InlineKeyboardMarkup([[ok_button]]) sender_name = store_name(sender) receiver_name = store_name(receiver) if receiver.is_bot: if chat.less is True: text = no_bots_allowed_phrase.split('\n')[0] else: text = no_bots_allowed_phrase mid = bot.send_message(chat_id=cid, text=text, reply_to_message_id=get_mid(message), reply_markup=ok_keyboard, parse_mode='html').message_id chat.mids = [mid] chat.save() return sender_id = str(sender.id) receiver_id = str(receiver.id) if sender_id == receiver_id: if chat.less is True: text = self_tacoing_phrase.split('\n')[0] else: text = self_tacoing_phrase mid = bot.send_message(chat_id=cid, text=text, reply_to_message_id=get_mid(message), parse_mode='html').message_id chat.mids = [mid] chat.save() return tacos_sent = len(re.findall(taco_emoji, message.text)) txt = message.text if message.entities is not None: for entity in message.entities: txt = txt[:entity.offset] + txt[entity.offset + entity.length:] txt = txt.replace(taco_emoji, '') user_comment = txt.lstrip(' ') if tacos.taco_balance is None: amounts = dict() amounts.update({sender_id: default_taco_amount}) amounts.update({receiver_id: default_taco_amount}) else: amounts = tacos.taco_balance if sender_id not in amounts.keys(): amounts.update({sender_id: default_taco_amount}) if receiver_id not in amounts.keys(): amounts.update({receiver_id: default_taco_amount}) if tacos_sent > amounts.get(sender_id): if chat.less is True: text = balance_low_phrase.split('\n')[0] else: text = balance_low_phrase if chat.autohide: msg = bot.send_message(chat_id=cid, text=text, reply_to_message_id=get_mid(message), parse_mode='html') time = datetime.datetime.now() + datetime.timedelta( minutes=chat.autohide_delay) sched.add_job(delete_message, 'date', run_date=time, args=[bot, msg]) else: msg = bot.send_message(chat_id=cid, text=text, reply_to_message_id=get_mid(message), reply_markup=ok_keyboard, parse_mode='html') chat.mids = [msg.message_id] chat.save() return amounts.update({sender_id: amounts.get(sender_id) - tacos_sent}) amounts.update({receiver_id: amounts.get(receiver_id) + tacos_sent}) if chat.less is True: comment = '' else: if tacos_sent < 3: comment = taco_transfer_comment_low elif tacos_sent > 9: comment = taco_transfer_comment_high else: comment = taco_transfer_comment_medium.format(receiver_name) if "@" in sender_name: sender_link = "https://t.me/{}".format(sender_name[1:]) else: sender_link = "tg://user?id={}".format(sender_id) if "@" in receiver_name: receiver_link = "https://t.me/{}".format(receiver_name[1:]) else: receiver_link = "tg://user?id={}".format(receiver_id) if tacos_sent == 1: phrase = taco_transfer_phrase.replace('tacos', 'taco') else: phrase = taco_transfer_phrase if len(user_comment) > 0: phrase += '<b>And said:</b>\n>>><code>{}</code>'.format(user_comment) if chat.autohide: msg = bot.send_message(chat_id=cid, text=phrase.format(sender_link, sender_name, tacos_sent, receiver_link, receiver_name, comment), parse_mode='html', disable_web_page_preview=True) time = datetime.datetime.now() + datetime.timedelta( minutes=chat.autohide_delay) sched.add_job(delete_message, 'date', run_date=time, args=[bot, msg]) else: msg = bot.send_message(chat_id=cid, text=phrase.format(sender_link, sender_name, tacos_sent, receiver_link, receiver_name, comment), reply_markup=ok_keyboard, parse_mode='html', disable_web_page_preview=True) chat.mids = [msg.message_id] chat.save() tacos.taco_balance = amounts tacos.save()
def schedule_url_crawler(): # every day at 9pm sched.add_job(crawl_by_district, 'cron', hour=18) # every day at 3am sched.add_job(crawl_by_metro_station, 'cron', hour=8)
def schedule_validator(): # every 15 minutes sched.add_job(validate_data, 'interval', minutes=15)
def taco_top_callback(bot, message): """ shows top-5(or less) taco-users in chat """ cid = get_cid(message) mid = get_mid(message) store_name(message.from_user) chat = Chats.get(Chats.cid == message.chat.id) clean_chat(chat.mids, chat.cid, bot, message) ok_button = InlineKeyboardButton('OK', callback_data='delete:{}'.format(message.from_user.id)) ok_keyboard = InlineKeyboardMarkup([[ok_button]]) tacos = Tacos.get(Tacos.chat == cid) balances = tacos.taco_balance if len(balances) == 0: if chat.autohide: msg = bot.send_message(text=empty_top_phrase, chat_id=cid, parse_mode='html') time = datetime.datetime.now() + datetime.timedelta(minutes=chat.autohide_delay) sched.add_job(delete_message, 'date', run_date=time, args=[bot, msg]) else: bot.send_message(text=empty_top_phrase, chat_id=cid, reply_markup=ok_keyboard, parse_mode='html') return top = list() while len(balances) > 0 and len(top) < 5: top_uid = max(balances, key=balances.get) username = resolve_name(top_uid) top.append([username, balances.get(top_uid)]) del balances[top_uid] formatted_top = '' for user in top: if "@" in user[0]: user_link = "https://t.me/{}".format(user[0][1:]) else: user_link = "tg://user?id={}".format(user[0]) formatted_top += '{}. <a href="{}">{}</a> - <code>{}</code> tacos!\n'.format(top.index(user) + 1, user_link, user[0][1:], user[1]) if chat.autohide: msg = bot.send_message(text=taco_top_phrase.format(len(top), formatted_top), chat_id=cid, parse_mode='html', disable_web_page_preview=True) time = datetime.datetime.now() + datetime.timedelta(minutes=chat.autohide_delay) sched.add_job(delete_message, 'date', run_date=time, args=[bot, msg]) else: bot.send_message(text=taco_top_phrase.format(len(top), formatted_top), chat_id=cid, reply_markup=ok_keyboard, parse_mode='html', disable_web_page_preview=True) chat.mids.append(mid) chat.save()