Skip to main content

Telebot listener

Project description

Installing

pip3 install -U tele-mod

Telemod

Message listener example

import asyncio

from telebot import async_telebot, types

from telemod import Listener



bot = async_telebot.AsyncTeleBot(token="")

loop = asyncio.get_event_loop()



listener = Listener(bot=bot, loop=loop)



@bot.message_handler(commands=["start"])

async def _(message: types.Message):

    msg = await listener.listen_to(m=message, text="What's Your name?")

    await bot.reply_to(msg, f"Your name is {msg.text}")

    # to delete question message

    # await bot.delete_message(chat_id=message.chat.id, message_id=msg.output.id)



async def main():

    print((await bot.get_me()).first_name)

    await bot.infinity_polling(skip_pending=True)



loop.run_until_complete(main())

CallbackQuery Listener Example

@bot.callback_query_handler(func = lambda query: True)

async def _(query: types.CallbackQuery):

    if query.data == "name":

        msg = await listener.listen_to(

            query,

            "What's your name?",

            filters=["text"]

        )

        return await bot.reply_to(msg, f"Your name is {msg.text}")

Simple usage example

msg = await listener.listen(

    chat_id=chat_id,

    text="What's your name",

    from_id=from_id,

    filters=["text", "photo"]

)

print(msg)

Time out example

@bot.message_handler(commands=["start"])

async def start_handler(message: types.Message):

    try:

        msg = await listener.listen_to(message, "What's your name?", timeout=10)

    except TimeOut:

        msg = None

        await bot.reply_to(message, "Time Out")

    if msg:

        return await bot.reply_to(msg, f"Hi {msg.text}")

Filters type example

@bot.message_handler(commands=["ask"], chat_types=["supergroup"])

async def _(message: types.Message):

    msg = await listener.listen(

        chat_id=message.chat.id,

        text="What's your name",

        filters=["sender_chat", "text"],

        filters_type=2

    ) # will only listen to an message has "sender_chat" and "text" attribute

    msg = await listener.listen(

        chat_id=message.chat.id,

        text="What's your name",

        filters=["sender_chat", "text"],

        filters_type=1

    ) # will listen to any messgae has "sender_chat" or "text" attribute

    return await bot.reply_to(msg, msg.text)

Community

Examples

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tele-mod-2.5.tar.gz (4.9 kB view hashes)

Uploaded Source

Built Distribution

tele_mod-2.5-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page