def main(): pp = PicklePersistence(filename='bidar.data') updater = Updater(TOKEN, persistence=pp, use_context=True) dp = updater.dispatcher states = [state.name for state in STATES] conv_handler = ConversationHandler( name='conv_handler', persistent=True, entry_points=[CommandHandler('start', start)], states={ DISPATCHER: [MessageHandler(Filters.text, dispatcher)], DELETE: [MessageHandler(Filters.text, delete)], CATEGORY: [MessageHandler(Filters.text, category)], SUBCATEGORY: [MessageHandler(Filters.text, sub_category)], STATE: [MessageHandler(Filters.text, state)], PRICE_FROM: [MessageHandler(Filters.text, price_from),], PRICE_TO: [MessageHandler(Filters.text, price_to),], METRAZH_FROM: [MessageHandler(Filters.text, metrazh_from),], METRAZH_TO: [MessageHandler(Filters.text, metrazh_to),], VADIE_FROM: [MessageHandler(Filters.text, vadie_from),], VADIE_TO: [MessageHandler(Filters.text, vadie_to),], EJARE_FROM: [MessageHandler(Filters.text, ejare_from),], EJARE_TO: [MessageHandler(Filters.text, ejare_to),], PLACES: [MessageHandler(Filters.text, places),], ATRAF: [MessageHandler(Filters.text, atraf),], }, fallbacks=[CommandHandler('cancel', start)] ) dp.add_handler(conv_handler) # log all errors dp.add_error_handler(error) # Start the Bot updater.start_polling() for user, data in pp.get_user_data().items(): for wd in data['watchdogs']: wd.start_job(updater.job_queue) # Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle()
def test_flush_on_stop(self, bot, update, pickle_persistence): u = Updater(bot=bot, persistence=pickle_persistence) dp = u.dispatcher u.running = True dp.user_data[4242424242]['my_test'] = 'Working!' dp.chat_data[-4242424242]['my_test2'] = 'Working2!' u.signal_handler(signal.SIGINT, None) del (dp) del (u) del (pickle_persistence) pickle_persistence_2 = PicklePersistence(filename='pickletest', store_user_data=True, store_chat_data=True, singe_file=False, on_flush=False) assert pickle_persistence_2.get_user_data( )[4242424242]['my_test'] == 'Working!' assert pickle_persistence_2.get_chat_data( )[-4242424242]['my_test2'] == 'Working2!'
def test_flush_on_stop_only_bot(self, bot, update, pickle_persistence_only_bot): os.remove('pickletest_user_data') os.remove('pickletest_chat_data') os.remove('pickletest_bot_data') u = Updater(bot=bot, persistence=pickle_persistence_only_bot) dp = u.dispatcher u.running = True dp.user_data[4242424242]['my_test'] = 'Working!' dp.chat_data[-4242424242]['my_test2'] = 'Working2!' dp.bot_data['my_test3'] = 'Working3!' u.signal_handler(signal.SIGINT, None) del (dp) del (u) del (pickle_persistence_only_bot) pickle_persistence_2 = PicklePersistence(filename='pickletest', store_user_data=False, store_chat_data=False, store_bot_data=True, single_file=False, on_flush=False) assert pickle_persistence_2.get_user_data() == {} assert pickle_persistence_2.get_chat_data() == {} assert pickle_persistence_2.get_bot_data()['my_test3'] == 'Working3!'
for e in G[node]: if G[node][e]['type'] == 'enquesta' and not alt: user_data['node'] = e elif G[node][e]['type'] == 'alternativa': if G[node][e]['resposta'] == res: user_data['alt'] = e user_data['isAlt'] = True procesar_node(bot, update, user_data) persist = PicklePersistence(filename='userdata.pickle') TOKEN = open('token.txt').read().strip() updater = Updater(token=TOKEN, persistence=persist) dispatcher = updater.dispatcher dispatcher.persistence = persist dispatcher.user_data = persist.get_user_data() dispatcher.add_handler(CommandHandler('start', start)) dispatcher.add_handler(CommandHandler('help', help)) dispatcher.add_handler(CommandHandler('author', author)) dispatcher.add_handler( CommandHandler('quiz', quiz, pass_user_data=True, pass_args=True)) dispatcher.add_handler( CommandHandler('bar', bar, pass_user_data=True, pass_args=True)) dispatcher.add_handler( CommandHandler('pie', pie, pass_user_data=True, pass_args=True)) dispatcher.add_handler(CommandHandler('report', report, pass_user_data=True)) dispatcher.add_handler( MessageHandler(Filters.text, procesar_resposta, pass_user_data=True)) updater.start_polling()
def main(): print("Starting bot") global pp, updater, globalsns, notifications globalsns = shelve.open(PERSISTENCE_GLOBAL_SNS_FILENAME, writeback=True) # Create the Updater and pass it your bot's token. pp = PicklePersistence(filename=PERSISTENCE_USER_FILENAME, store_user_data=True, store_chat_data=False, on_flush=True) for uid, data in pp.get_user_data().items(): if 'notify_about' in data: for pubkey in data['notify_about']: if pubkey not in notifications: notifications[pubkey] = set() notifications[pubkey].add(uid) updater = Updater(TELEGRAM_TOKEN, persistence=pp, user_sig_handler=stop_rta_thread) start_rta_update_thread() # Get the dispatcher to register handlers dp = updater.dispatcher updater.dispatcher.add_handler( CommandHandler('start', start, pass_user_data=True)) updater.dispatcher.add_handler( CommandHandler('dist', show_dist, pass_user_data=True)) if WALLET_RPC and TESTNET: updater.dispatcher.add_handler( CommandHandler('send', send_stake, pass_user_data=True, pass_args=True)) updater.dispatcher.add_handler( CommandHandler('balance', balance, pass_user_data=True)) updater.dispatcher.add_handler( CommandHandler('donate', donate, pass_user_data=True)) updater.dispatcher.add_handler( CommandHandler('sn', show_sn, pass_user_data=True, pass_args=True)) updater.dispatcher.add_handler( CommandHandler('track', track_sn, pass_user_data=True, pass_args=True)) updater.dispatcher.add_handler( CommandHandler('tracking', show_tracking, pass_user_data=True, pass_args=True)) updater.dispatcher.add_handler( CommandHandler('sample', show_sample, pass_user_data=True, pass_args=True)) updater.dispatcher.add_handler( CommandHandler('height', show_height, pass_user_data=True, pass_args=True)) updater.dispatcher.add_handler( CommandHandler('nodes', show_nodes, pass_user_data=True, pass_args=True)) updater.dispatcher.add_handler( CommandHandler('snodes', show_snodes, pass_user_data=True, pass_args=True)) updater.dispatcher.add_handler( CommandHandler('myid', my_id, pass_user_data=True)) updater.dispatcher.add_handler( CommandHandler('chatid', chat_id, pass_user_data=True)) updater.dispatcher.add_handler( CommandHandler('slap', slap, pass_user_data=True)) updater.dispatcher.add_handler( MessageHandler(Filters.sticker, sticker_input, pass_user_data=True)) # log all errors dp.add_error_handler(error) # Start the Bot updater.start_polling() print("Bot started") # Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle() print("Saving persistence and shutting down") pp.flush() globalsns.close()
def xmas_bot(token): pp = PicklePersistence(filename="xmas_reminder_bot_data") updater = Updater(token, persistence=pp, use_context=True) conv_handler = ConversationHandler( entry_points=[CommandHandler('settings', settings_cmd)], states={ CHOOSE: [CallbackQueryHandler(chooseSetting)], HANDLE_XMAS: [CallbackQueryHandler(handleXmasDay)], HANDLE_REMINDER: [CallbackQueryHandler(handleReminderTime)] }, fallbacks=[CommandHandler('cancel', cancel_cmd)]) myDispatcher = updater.dispatcher myDispatcher.add_handler(conv_handler) myDispatcher.add_handler(CommandHandler("about", about_cmd)) myDispatcher.add_handler(CommandHandler("cancel", cancel_cmd)) myDispatcher.add_handler(CommandHandler("help", help_cmd)) myDispatcher.add_handler(CommandHandler("howlong", howlong_cmd)) myDispatcher.add_handler(CommandHandler("start", start_cmd)) myDispatcher.add_handler(CommandHandler("stop", stop_cmd)) myDispatcher.add_handler(CommandHandler("restart", restart_cmd)) # message handler for send a tree, get a tree myDispatcher.add_handler( MessageHandler(Filters.sticker | (Filters.text & ~Filters.command), chatWithSanta)) # stored user data user_data = pp.get_user_data() for item in pp.get_chat_data().items(): chat_id = item[0] this_user = user_data[chat_id] if this_user.get("reminders", "off") == "on": # new job here = timezone('Europe/Berlin') (hours, minutes) = this_user['reminder_time'].split(":") localized = here.localize( dt.datetime.combine(dt.date.today(), dt.time(int(hours), int(minutes), 0))) updater.job_queue.run_daily(reminder, localized, context={ "chat_id": chat_id, "xmas_day": this_user["xmas_day"] }, name=str(chat_id)) if newerVersionExists(this_user.get("version", "1.0.2")): # we have a newer version -> show what's new text = "Santa has been innovating. Here's what's new in "\ "version %s %s\n\n" % (CURRENT_VERSION, E_tada) for note in Whats_new[CURRENT_VERSION]: text = text + ("\t\U00002022 %s\n" % note) # send a what's new message updater.job_queue.run_once(whatsNewMessage, 2, context={ "chat_id": chat_id, "text": santaSay(text) }, name="new%s" % str(chat_id)) # then, update current version myDispatcher.user_data[chat_id]["version"] = CURRENT_VERSION updater.start_polling() updater.idle()
" 牌河|"+tomahjong(i.riverTile)+"\n" #printjson({"name": i.name, "riverTile": i.riverTile}) m=player.message["boardm"] m.edit_text(info) boards = [] # =========================================================bot部分 my_persistence = PicklePersistence(filename='userfile') userdata=my_persistence.get_user_data () #print(userdata) #清空玩家状态 for i in userdata.keys(): userdata[i].pop("confirm","") userdata[i].pop("option","") userdata[i].pop("state","") userdata[i].pop("board","") my_persistence.update_user_data(i,userdata[i]) # userdata=my_persistence.get_user_data () # print(userdata) updater = Updater(token='1723327297:AAHMHOYjfXYNdlJNMDoLLU-Bx74MxivZOfk', persistence=my_persistence,use_context=True, request_kwargs={'proxy_url': 'http://127.0.0.1:7890/'}) dispatcher = updater.dispatcher def start(update, context):
from telegram.ext import PicklePersistence import psycopg2, psycopg2.extras import sys data = PicklePersistence(filename="lokisnbot.data", store_user_data=True, store_chat_data=False, on_flush=True) pgsql = psycopg2.connect(dbname='lokisnbot', connection_factory=psycopg2.extras.LoggingConnection) pgsql.initialize(sys.stderr) for telegram_id, d in data.get_user_data().items(): cur = pgsql.cursor() cur.execute("INSERT INTO users (telegram_id) VALUES (%s) RETURNING id", (telegram_id, )) uid = cur.fetchone()[0] for sn in (d['sn'] if 'sn' in d else []): sn_row = { 'pubkey': None, 'active': False, 'complete': False, 'expires_soon': False, # True now, but was false before the PGSQL rewrite 'last_contributions': None, 'last_reward_block_height': None, 'alias': None, 'note': None, 'notified_dereg': False, 'notified_uptime_age': None,