def gen_keyboard(s, **d): kb_data = d.get('kb') def gen_handler(name, desc): return lambda: (States.ITEM, {"ItemText": name + "\n" + desc}) keyboard = { # the format is ad in UI - {"NAME":tgf.action} name: tgf.action(gen_handler(name, desc)) for name, desc in kb_data } # forward to THANKS state and update data. Upd_id is used in THANKS print(keyboard) return keyboard
UI={ States.HOME:{ 't':'Welcome, buddy. Wanna stay super-productive?', 'b':[ {'Start activity':a(States.ACTIVITY,update_msg=False)}, {'View Stats':a(States.NOT_IMPLEMENTED)}, {'Add new goal':a(States.NOT_IMPLEMENTED)}, ], #'kb_txt':"Welcome!", #'kb':h.obj(home_kb) }, States.START:{ 't':'Notification polling started', 'b':[ { 'to home':tgf.action(States.HOME)} ], 'prepare':start_notifications_longpoll }, States.ACTIVITY:{ 't':"Here are your Goals", 'b':[ ps(lambda s,**d: d.get('GoalButtons')), {} ], 'prepare':get_activities }, States.GOAL:{ 't':h.st("You are about to work on Goal %s",'GoalName'), 'b':[ {'Start pomodoro':a(start_pomodoro)}
lg("cancelling. data", d) schedule.cancel_job(d.get('job')) lg("gobs running", schedule.jobs) return 'start' UI = { 'start': { 't': 'hello there!', 'b': [{ 'create sch': lambda i: 'get_time' }] }, 'get_time': { 't': 'send me minutes interval', 'react': tgf.action(set_sc, react_to='text') }, 'cancel': { 't': tgf.paste('cancel sched %i', 'time'), 'b': [{ 'cancel': tgf.action(cancel) }] } } def runs(): while True: schedule.run_pending() time.sleep(1)
name: tgf.action(gen_handler(name, desc)) for name, desc in kb_data } # forward to THANKS state and update data. Upd_id is used in THANKS print(keyboard) return keyboard UI = { States.START: { 't': 'Hello! Follow the button to display list of items', 'b': [ # this button will appear to every user, triggers the add_kb func { 'Display list': tgf.action(States.LIST) }, ] }, States.LIST: { 't': 'Here is your items list', 'b': [ # return value from gen_keyboard will be put here # in runtime tgf.post(gen_keyboard), { 'back': tgf.action(States.START) } ], # Before processing state db call will be executed
return States.SUCCESS, {} def get_all_data(i, s, **d): data = db_api.get_all_data(d['sheet']) return States.GET, {'data': data} UI = { States.START: { 'text': ('Hello, I can help you work with Google Spreadsheets. ' 'Share your sheet with [email protected].' 'Then just send me your spreadsheet name and let\'s get started!'), 'react': tgflow.action(open_sheet, react_to='text'), 'prepare': [ analytics.send_pageview, bitrix.add_lead, bitrix.add_contact, bitrix.add_deal ], }, States.CHOOSE: { 'text': 'What should I do?', 'buttons': [{ 'Insert row': tgflow.action(States.PUT) }, { 'Recieve all data': tgflow.action(get_all_data) }], 'prepare': [ analytics.send_pageview,
import tgflow as tgf from tgflow import handles as h from tgflow.api.cli import cliAPI from enum import Enum class States(Enum): START=1 INFO=2 BALOON = 3 ui = { States.START:{ 't':'Hello there!', 'b':[ {'show my info':tgf.action(States.INFO)}, ], 'prepare':lambda i,s,**d: {'info':"Custom start info"}, }, States.INFO:{ 't':tgf.post(lambda s,**d: d['info']), 'b': [ {'back to start':tgf.action(States.START)}, {'make a baloon':tgf.action(States.BALOON)}, ] }, States.BALOON:{ 't':""" _-.:.-_ .'-/_:-;_\- . /_'/__ |__'._'\\
def bot(netconf): net = ConnectorNetwork(netconf, appid='0', name='telegram') db = DB() def handle_notif(notif): str_notif = json.dumps(notif) try: user_id = str(notif['user_id']) tgid = db.get_tg_id(user_id) except Exception as e: _print("Notif was not sent", e) return "FAIL" _print("got notif:", str_notif) message = "Got new notif of type %s. Content: %s" % ( notif.get('type'), notif.get('content')) if not tgid: print("User id %s has no telegram log" % user_id) return "FAIL" try: tgf.send_raw(message, tgid) except Exception as e: _print("Notif was not sent", e) return "FAIL" return 'OK' net.listen_for_notif(handle_notif) def login_uid_1(i): telegram_id = i.message.chat.id user_id = '1' db.save_tg_id(user_id, telegram_id) return States.action, {'user_id': user_id} def handle_action(i, user_id=None): _print('inp', i) if not user_id: _print('user not logged in') return States.login text = i.text msg_type = 'telegram' try: msg_type, content = text.split('\\') except ValueError: content = text message = { 'type': msg_type, 'content': content, 'user_id': user_id, } net.send(message) # stay silent return -1 UI = { States.action: { 't': 'Enter an action type and content to send', 'b': [{ "Settings": tgf.action(States.settings) }], 'react': tgf.action(handle_action, react_to='text') }, States.settings: { 't': 'Settings', 'b': [{ "Action": tgf.action(States.action) }], }, States.start: { 't': 'Welcome!', 'b': [ { "Log in": tgf.action(States.login) }, ] }, States.login: { 't': 'Please log in', 'b': [{ "Log in as 1": tgf.action(login_uid_1) }], } } key = '539066078:AAHCUsr8ZoP9JtP5KqOMuL7f_UoFyyH6wik' tgf.configure( token=key, state=States.start, #apiModel=cliAPI, verbose=True, ) tgf.start(UI)
keyboard.update({ # the format is ad in UI - {"NAME":tgf.action} but_name: h.action(gen_handler(but_name)) }) # forward to THANKS state and update data. Upd_id is used in THANKS return States.THANKS, {'keyboard': keyboard, 'upd_id': but_name} UI = { States.START: { 't': 'Hello! click on a button to create or modify', 'b': [ # this button will appear to every user, triggers the add_kb func { 'create button': tgf.action(add_kb) }, # this post will return data['keyboard'] or {} if no such key ## again, data is dict where all user's variables are stored h.post(lambda s, **d: d.get('keyboard', {})) # Some fancy magic will be done by tgflow above. # Before this state is passed, all h.post will be evaluated. # This h.post will return an array of buttons # tgflow will automatically add the persistent button to # ones that are returned by h.post! ] }, States.MODIFY_BUT: { 't': 'Send me the name of button',
key = '539066078:AAHCUsr8ZoP9JtP5KqOMuL7f_UoFyyH6wik' class States(Enum): START = 1 INFO = 2 FAV = 3 THANKS = 4 UI = { States.START: { 't': 'hello', 'b': [{ 'show info': tgf.action(States.INFO) }, { 'set my favourite': h.action(States.FAV) }] }, States.INFO: { 't': 'Please send me name of your favourite thing', 'b': [{ 'set foo': (lambda input, data: (States.THANKS, { 'foo': 'var' + data['foo'] })) }] }, States.FAV: { 't':
key = '539066078:AAHCUsr8ZoP9JtP5KqOMuL7f_UoFyyH6wik' class States(Enum): START = 1 INFO = 2 FAV = 3 THANKS = 4 UI = { States.START: { 't': 'Hello, User!\n Here\'s what you can do:', 'b': [{ 'show my info': tgf.action(States.INFO) }, { 'set my favourite': h.action(States.FAV) }] }, States.FAV: { 't': 'Please send me name of your favourite thing', 'b': [{ 'my favourite is star': (lambda input, data: (States.THANKS, { 'fav': 'star' })) }], 'react': h.action(lambda i: (States.THANKS, { 'fav': i.text
return States.NOTIF, {'delay': delay} class States(Enum): START = 1 REMIND = 2 NOTIF = 3 SET = 4 UI = { States.START: { 't': 'Hi! You can set up a reminder here.', 'kb': [ { 'Set up a reminder': tgf.action(States.REMIND) }, ] }, States.REMIND: { 't': 'Please send me a delay in seconds:', 'b': [{ 'Back': tgf.action(States.START) }], 'react': h.action(handle_delay, react_to='text'), }, States.SET: { 't': h.st('Delay is %s', 'delay'), 'b': [ { 'back to start': tgf.action(States.START)
else: if sql.passPols(login) == i.text: check = 1 if check: return States.LOGGED else: tgf_obj.bot.send_message(i.chat.id, 'Неверный логин или пароль') return States.LOGIN UI = { States.START: { 't': 'Здравствуйте. Вы уже подключены к нашим услугам?', 'b': [ { 'Да': tgf.action(States.LOGIN) }, { 'Нет': h.action(States.INFO) }, ], 'react': h.action(geo, react_to='location'), }, States.INFO: { 't': 'Возможно вас заинтересуют наши услуги', 'b': [ { 'Просмотреть': tgf.action(uslugi1) }, {
check = 0 else: if '123456' == i.text: check = 1 if check: return States.LOGGED else: tgf_obj.bot.send_message(i.chat.id, 'Неверный логин или пароль') return States.LOGIN UI = { States.START: { 't': 'Добрый день. Вас приветствует система HealthAI', 'b': [{ 'Войти в систему': tgf.action(States.LOGIN) }], 'react': h.action(qr, react_to='photo'), }, States.LOGGED: { 't': 'Личный кабинет', 'b': [ { 'Отсканировать QR': tgf.action(States.QR) }, { 'Список лекарств': tgf.action(stat, update_msg=False) }, { 'Отправить фото': tgf.action(States.TAB)