Ejemplo n.º 1
0

@router.command_handler("/quiz")
def on_show_quiz_poll(bot, message):
    bot.send_poll(
        chat_id=message.chat.id,
        question="quiz",
        options=("answer 1", "answer 2", "answer 3"),
        is_anonymous=False,
        type=PollType.QUIZ,
        correct_option_id=2,
    )
    return bot.stop_call


@router.poll_answer_handler()
def on_poll_answer(bot, poll_answer):
    bot.send_message(chat_id=poll_answer.user.id,
                     text="you select: {0}".format(poll_answer.option_ids))
    print(poll_answer)
    return bot.stop_call


async def on_update(bot, update):
    await router.dispatch(bot, update)


bot = bot_client.create_bot(token=BOT_TOKEN)
bot.delete_webhook(drop_pending_updates=True)
bot.run_polling(on_update, timeout=10)
Ejemplo n.º 2
0
    },
}

# or using gettext
# import gettext
# trans_data = {}
# locale_dir = "./locales"
# for lang in ("en", "zh-hant"):
#     translate = gettext.translation("example", locale_dir, languages=[lang])
#     translate.install()
#     trans_data[lang] = translate

router = bot_client.router()


@router.message_handler(MessageField.TEXT)
@i18n()
def on_i18n_reply(bot, message, _):
    bot.reply_message(message, text=_(message.text))
    return bot.stop_call


async def on_update(bot, update):
    await router.dispatch(bot, update)


bot = bot_client.create_bot(token=BOT_TOKEN, i18n_source=trans_data)

bot.delete_webhook(drop_pending_updates=True)
bot.run_polling(on_update, timeout=10)
Ejemplo n.º 3
0
db_conn = sqlite3.connect("file:memory?cache=shared&mode=memory", uri=True)
storage = SQLiteStorage(db_conn)


# using redis
# import redis
# from telegrambotclient.storage import RedisStorage
# redis_client = redis.StrictRedis(
#    host="127.0.0.1",
#    port=6379,
#    password="",
#    db=1,
#    max_connections=10,
#    decode_responses=True,
# )
# storage = RedisStorage(redis_client)

# using mongodb
# from pymongo import MongoClient
# storage = MongoDBStorage(
#     MongoClient("mongodb://localhost:27017")["session_db"]["session"])
async def on_update(bot, update):
    await router.dispatch(bot, update)


bot = bot_client.create_bot(
    token=BOT_TOKEN, storage=storage,
    session_expires=300)  # set 300s as a session timeout
bot.delete_webhook(drop_pending_updates=True)
bot.run_polling(on_update, timeout=10)
Ejemplo n.º 4
0
from telegrambotclient.base import MessageField, ParseMode

BOT_TOKEN = "<BOT_TOKEN>"

router = bot_client.router()


@router.message_handler(MessageField.TEXT)
def on_echo_text(bot, message):
    bot.reply_message(
        message,
        text="I receive: <strong>{0}</strong>".format(message.text),
        parse_mode=ParseMode.HTML,
    )
    return bot.stop_call


async def on_update(bot, update):
    await router.dispatch(bot, update)


# the bot with the offical api
bot = bot_client.create_bot(token=BOT_TOKEN)
bot.delete_webhook(drop_pending_updates=True)
if bot.log_out():
    bot = bot_client.create_bot(
        token=BOT_TOKEN,
        bot_api=TelegramBotAPI(
            host="http://your_local_api_host"))  # self-define api provider
    bot.run_polling(on_update, timeout=10)
Ejemplo n.º 5
0
router1 = bot_client.router(BOT_TOKEN_1)


def on_message(bot, message):
    pretty_print(message)
    bot.send_message(chat_id=message.chat.id,
                     text="bot {0} receives a message: {1}".format(
                         bot.user.id, message.text))


router0.register_message_handler(on_message, MessageField.TEXT)
router1.register_message_handler(on_message)  # not only text message

app = FastAPI()


@app.post("/{bot_token}", status_code=status.HTTP_200_OK)
async def serve_update(bot_token: str, request: Request):
    bot = bot_client.bots.get(bot_token, None)
    if bot:
        router = bot_client.routers.get(bot_token, None)
        if router:
            await router.dispatch(bot, TelegramObject(**await request.json()))
    return "OK"


bot_client.create_bot(token=BOT_TOKEN_0).set_webhook(
    url=WEBHOOK_URL.format(BOT_TOKEN_0))
bot_client.create_bot(token=BOT_TOKEN_1).set_webhook(
    url=WEBHOOK_URL.format(BOT_TOKEN_1))