Giter Site home page Giter Site logo

aiotg's Introduction

aiotg

image

Asynchronous Python API for building Telegram bots, featuring:

  • Easy and declarative API
  • Hassle-free setup - no need for SSL certificates or static IP
  • Built-in support for analytics via botan.io
  • Automatic handling of Telegram API throttling or timeouts

Install it with pip:

pip install aiotg

Then you can create a new bot in few lines:

from aiotg import Bot

bot = Bot(api_token="...")

@bot.command(r"/echo (.+)")
def echo(chat, match):
    return chat.reply(match.group(1))

bot.run()

Now run it with a proper API_TOKEN and it should reply to /echo commands.

The example above looks like a normal synchronous code but it actually returns a coroutine. If you want to make an external request (and that's what bots usually do) just use aiohttp and async/await syntax:

import aiohttp
from aiotg import Bot

bot = Bot(api_token="...")

@bot.command("bitcoin")
async def bitcoin(chat, match):
    url = "https://api.bitcoinaverage.com/ticker/global/USD/"
    async with aiohttp.get(url) as s:
        info = await s.json()
        await chat.send_text(info["24h_avg"])

bot.run()

But what if you just want to write a quick integration and don't need to provide a conversational interface? We've got you covered! You can send messages (or any other media) by constructing a Chat object with user_id or channel name. We even saved you some extra keystrokes by providing handy Channel constructors:

...
channel = bot.channel("@yourchannel")
private = bot.private("1111111")

async def greeter():
    await channel.send_text("Hello from channel!")
    await private.send_text("Why not greet personally?")
...

For a more complete example, take a look at WhatisBot or Music Catalog Bot.

For more information on how to use the project, see the project's documentation. Have a question? Ask it on project's Telegram chat.

aiotg's People

Contributors

szastupov avatar shadrus avatar olehbozhok avatar spirkaa avatar vijfhoek avatar bliz937 avatar

Watchers

James Cloos avatar Stasinskii avatar Alexander Miliutin 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.