def test_doubleblueprintability_and_execute(self): states_tbp = TBlueprint(__name__) states_tbp2 = TBlueprint(__name__ + "2") states_tbp.register_tblueprint(states_tbp2) states_drvr = SilentDriver() states = TeleStateMachine(__name__, database_driver=states_drvr, teleflask_or_tblueprint=states_tbp2) states.teleflask._bot = BotMock('FAKE_API_KEY', return_python_objects=True) called = [False] @states.DEFAULT.command('cancel') def func_1(update, text): called[0] = True # end def bot = Teleflask('FAKE_API_KEY', app=None, hostname="localhost", debug_routes=False, disable_setting_webhook_telegram=True, disable_setting_webhook_route=True) bot._bot = BotMock('FAKE_API_KEY', return_python_objects=True) bot.init_bot() bot.register_tblueprint(states_tbp) bot.process_update(update1) self.assertTrue(called[0], 'func_1 should have been called')
def test_blueprintability_and_register(self): states_tbp = TBlueprint(__name__) states_drvr = SilentDriver() states: TeleStateMachine = TeleStateMachine( __name__, database_driver=states_drvr, teleflask_or_tblueprint=states_tbp) states.teleflask._bot = BotMock('FAKE_API_KEY', return_python_objects=True) states.teleflask.init_bot() @states.DEFAULT.command('cancel') def func_1(update): pass # end def bot = Teleflask('FAKE_API_KEY', app=None, hostname="localhost", debug_routes=False, disable_setting_webhook_telegram=True, disable_setting_webhook_route=True) bot._bot = BotMock('FAKE_API_KEY', return_python_objects=True) bot.init_bot() bot.register_tblueprint(states_tbp) self.assertGreater(len(states.CURRENT.update_handler.commands), 0, 'should have added an command.')
def setUp(self): os.environ.setdefault("URL_HOSTNAME", "example.com") app = Flask(__name__) app.config.from_pyfile("testconfig.py") self.app = app self.app_test = app.test_client() # get a test client self.bot = Teleflask(API_KEY, return_python_objects=True) # replace the :class:`pytgbot.Bot` instance with something testable. (not using TG server) # All methods now return the stuff they would sent to the telegram servers as json instead. # This is not usable with the :class:`teleflask.message.Message` (sub)types. self.bot.bot = BotTestable( API_KEY, return_python_objects=self.bot._return_python_objects) # Init must be before replacing the ``bot.bot``, # because else the :meth:`Bot.getWebhook` won't work as expected in startup. self.bot.init_app(app) # Array to hold information about the called callbacks: self.callbacks_status = {} # Get the update_path to be able to test via emulating incoming updates self.update_path, self.update_url = self.bot.calculate_webhook_url( hostname=self.bot.hostname, hostpath=self.bot.hostpath, hookpath=self.bot.hookpath)
def setUp(self): self.b = Teleflask(api_key=None, app=None, hostname="localhost", debug_routes=False, disable_setting_webhook_telegram=True, disable_setting_webhook_route=True) self.b._bot = BotMock('FAKE_API_KEY', return_python_objects=True) self.d = SilentDriver() self.m = TeleStateMachine(__name__, self.d, self.b) self.s = TeleState('LITTLEPIP') self.b.init_bot()
API_KEY = os.getenv('TG_API_KEY', None) assert (API_KEY is not None) # TG_API_KEY environment variable URL_HOSTNAME = os.getenv('URL_HOSTNAME', None) # URL_HOSTNAME environment variable, can be None URL_PATH = os.getenv('URL_PATH', None) assert (URL_PATH is not None) # URL_PATH environment variable logger = logging.getLogger(__name__) logging.add_colored_handler(level=logging.DEBUG) app = Flask(__name__) bot = Teleflask(API_KEY, hostname=URL_HOSTNAME, hostpath=URL_PATH, hookpath="/income/{API_KEY}") bot.init_app(app) @app.errorhandler(404) def url_404(error): return "404.", 404 # end def @app.route("/", methods=["GET", "POST"]) def url_root(): return '<b>Hello world</b> from your <a href="https://github.com/luckydonald/docker-telegram-bot/">flask based telegram bot</b>.<br>' \
from .utils import to_json_remove_api_key, msg_get_reply_params from .secrets import API_KEY, EVENT_CHANNEL from .commands import bot as commands_tbp from .features import bot as features_tbp from .gitinfo import bot as versions_tbp __author__ = 'luckydonald' logger = logging.getLogger(__name__) logging.add_colored_handler(level=logging.DEBUG) app = Flask(__name__) bot = Teleflask(API_KEY, app) bot.register_tblueprint(commands_tbp) bot.register_tblueprint(features_tbp) bot.register_tblueprint(versions_tbp) @app.route("/info/<api_key>/<command>") @to_json_remove_api_key def info(api_key, command): """ Issue commands. E.g. /info/getMe :param command: :return:
# -*- coding: utf-8 -*- from teleflask import Teleflask from somewhere import API_KEY from luckydonaldUtils.logger import logging logger = logging.getLogger(__name__) __author__ = 'luckydonald' bot = Teleflask(API_KEY) @bot.command("test") def test(update, text): return "You tested with {arg!r}".format(arg=text) # end def
import os from flask import Flask, jsonify, request from teleflask import Teleflask import datetime ### print("oi") print(os.environ["API_KEY"]) app = Flask(__name__) bot = Teleflask(os.environ["API_KEY"]) print(bot) bot.init_app(app) ### print("ooi") last_prediction_time = None last_prediction_decision = False last_predicition_leave_time = None # Register the /start command @bot.command("start") def start(update, text): return TextMessage("<b>Oi, porra!</b>", parse_mode="html")
g ) from teleflask import Teleflask from teleflask.messages import MarkdownMessage from hashlib import sha1 import requests import dataset import shlex import random import re from distutils.version import StrictVersion __version__ = '0.2.0' app = Flask(__name__) bot = Teleflask(os.environ.get('TG_TOKEN'), app) DATABASE_URL = os.environ.get('DATABASE_URL', "sqlite:///data.db") url_regex = re.compile(r"^(https?:\/\/[\w\-\.]+\.[a-z]{2,20}\/[\w\-]+\/[\w\-]+)$", re.I) def get_db(): db = getattr(g, '_database', None) if db is None: db = g._database = dataset.connect(DATABASE_URL) return db def init_db(): with app.app_context(): db = get_db() fresh_db = False