'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 'prepare': database_call }, States.ITEM: { 't': tgf.st('Item: %s', 'ItemText'), 'b': [ { 'back to List': tgf.action(States.LIST) }, ], }, } tgf.configure( token=key, state=States.START, #apiModel = cliAPI, ) tgf.start(UI)
import tgflow as tgf import threading from UI import goals, notifs, login, misc, home from StateMachine import States from DataBase import db from tgflow.api.cli import cliAPI from telebot import apihelper #apihelper.proxy = { #'https':'socks5://localhost:9050'} #apihelper.CONNECT_TIMEOUT=20 lg = log.bot.info print('Hello. Starting bot') tgf.configure( token=config.token, #apiModel=cliAPI, state=States.START) UI = {} UI.update(misc.UI) UI.update(home.UI) UI.update(login.UI) UI.update(goals.UI) UI.update(notifs.UI) print(UI) tgf.start(UI)
import tgflow as tgf import re, urllib from enum import Enum key='539066078:AAHCUsr8ZoP9JtP5KqOMuL7f_UoFyyH6wik' class States(Enum): START=1 RESULT=2 UI = { States.START: {'t':'Hello! You can send me some text and I will google it', 'react':tgf.HTTPAction( 'http://google.com/search?q=%(_text)s', States.RESULT, react_to = 'text'), }, States.RESULT:{ 't':tgf.st("result is %s",'result'), 'prepare':lambda i,s,**d: {'result': re.search( '"r"><a href=".+?q=(.*?)&.+?">(.*?)<\/a></h3>', d['http_result'] ).group(1) } } } tgf.configure(token=key, verbose=True, state=States.START) tgf.start(UI)
import config import log import tgflow as tgf import schedule, time import threading lg = log.bot.info tgf.configure(token=config.token, state='start') def sch(i): def send(): print('sending') tgf.send_state('notif', i.chat.id) #t = threading.Timer(sec, send) #t.start() #s.enter(sec,1,send) #s.run(blocking=False) lg('exit sch') return send def set_sc(i): lg('sceduling every %i min' % int(i.text)) j = schedule.every(int(i.text)).minutes.do(sch(i)) return 'cancel', {'time': int(i.text), 'job': j} def cancel(d):
analytics.send_pageview, bitrix.update_deal(bitrix_stages_dict[States.SUCCESS]) ] }, States.GET: { 'text': tgflow.handles.st('Here is your data:\n%s', 'data'), 'buttons': [{ 'Continue': tgflow.action(States.CHOOSE) }], 'prepare': [ analytics.send_pageview, bitrix.update_deal(bitrix_stages_dict[States.GET]) ] }, States.ERROR: { 'text': 'Sorry there was an error', 'buttons': [{ 'Start': tgflow.action(States.START) }], 'prepare': [ analytics.send_pageview, bitrix.update_deal(bitrix_stages_dict[States.ERROR]) ] } } tgflow.configure(token=key, state=States.START, verbose=True) tgflow.start(UI)
{'make a baloon':tgf.action(States.BALOON)}, ] }, States.BALOON:{ 't':""" _-.:.-_ .'-/_:-;_\- . /_'/__ |__'._'\\ '__(__tgflow )_ ' (__(___ )___ )__) .__(___.(__ )_ . \__\__ )__ /__/ -__\ _(_ -_. \ _\_)./_/ \_.|_./ |_|_| _ [_] """, 'b':[ {'to start':tgf.action(States.START)} ] }, } tgf.configure(token='0', state=States.START, apiModel=cliAPI, ) tgf.start(ui)
# 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', 'b': [{ 'cancel': tgf.action(States.START) }], # now every time user sends us some text, upd_kb will be called 'react': h.action(upd_kb, react_to='text'), }, States.THANKS: { 't': h.st('Youve modified, new name is %s', 'upd_id'), 'b': [ { 'back to start': tgf.action(States.START) }, ], # this is to stop triggering the previously defibed actiion for upd_kb 'clear_trig': 'text' }, } tgf.configure( token=key, state=States.START, ) tgf.start(UI)
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)
}), react_to='text'), }, States.THANKS: { 't': h.st('Thanks! I will remember it foo %s', 'foo'), 'b': [{ 'show info': h.action(States.INFO, update_msg=True) }, { 'set another favourite': h.action(States.FAV, update_msg=True) }], 'clear_trig': 'text' }, } tgf.configure(token=key, state=States.START, data={"foo": 'bar'}) tgf.start(UI) """ from tgflow import TgFlow from enum import Enum key='539066078:AAHCUsr8ZoP9JtP5KqOMuL7f_UoFyyH6wik' class States(Enum): START=1 TgFlow.configure(token=key, state=States.START, data={"foo":'bar'}) TgFlow.start({States.START:{'t':'hello'}}) """
class States(Enum): START = 1, WEATHER = 2 UI = { States.START: { 't': 'Это бот, который показывает погоду по геопозиции! Выберите нужную точку на карте, чтобы узнать там текущую погоду!', "react": h.action(sendLocation, react_to='location') }, States.WEATHER: { "react": h.action(sendLocation, react_to='location') } } tgf.configure(token=telegram_key, state=States.START) tgf.start(UI) """ from tgflow import TgFlow from enum import Enum telegram_key='539066078:AAHCUsr8ZoP9JtP5KqOMuL7f_UoFyyH6wik' class States(Enum): START=1 TgFlow.configure(token=telegram_key, state=States.START, data={"foo":'bar'}) TgFlow.start({States.START:{'t':'hello'}}) """
'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) }, ], 'clear_trig': 'text', }, States.NOTIF: { 't': h.st('Hey! time\'s up! delay was: %s', 'delay'), 'b': [ { 'back to start': tgf.action(States.START) }, ], 'clear_trig': 'text' }, } tgf.configure(token=key, verbose=True, state=States.START, data={"delay": 'unin'}) tgf.start(UI)