Git Product home page Git Product logo

aiogram_broadcaster's Introduction

PyPI version Python Aiogram CI

Aiogram Broadcaster

A simple and straightforward broadcasting implementation for aiogram

Installaiton

$ pip install aiogram-broadcaster

Examples

Few steps before getting started...

  • First, you should obtain token for your bot from BotFather and make sure you started a conversation with the bot.
  • Obtain your user id from JSON Dump Bot in order to test out broadcaster.

Note: These and even more examples can found in examples/ directory

Base usage

from aiogram_broadcaster import TextBroadcaster

import asyncio


async def main():

    # Initialize a text broadcaster (you can directly pass a token)
    broadcaster = TextBroadcaster('USERS IDS HERE', 'hello!', bot_token='BOT TOKEN HERE')
    
    # Run the broadcaster and close it afterwards
    try:
        await broadcaster.run()
    finally:
        await broadcaster.close_bot()


if __name__ == '__main__':
    asyncio.run(main())

Embed a broadcaster in a message handler

from aiogram import Bot, Dispatcher, types

from aiogram_broadcaster import MessageBroadcaster

import asyncio


async def message_handler(msg: types.Message):
    """
    The broadcaster will flood to a user whenever it receives a message
    """
    
    users = [msg.from_user.id] * 5  # Your users list
    await MessageBroadcaster(users, msg).run()  # Run the broadcaster


async def main():

    # Initialize a bot and a dispatcher
    bot = Bot(token='BOT TOKEN HERE')
    dp = Dispatcher(bot=bot)

    # Register a message handler
    dp.register_message_handler(message_handler, content_types=types.ContentTypes.ANY)
    
    # Run the bot and close it afterwards
    try:
        await dp.start_polling()
    finally:
        await bot.session.close()


if __name__ == '__main__':
    asyncio.run(main())

aiogram_broadcaster's People

Contributors

f0rzend avatar progtimes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.