Skip to main content

Autogram

Autogram is a telegram bot-API wrapper written in python3, with a keen focus on remaining stupidly simple.

QuickStart

pip install autogram

  • Copy either the Functional or OOP example below.
  • Run the bot the first time to generate project-name.json config template
  • Add your telegram bot token in the config, (from telegram:BotFather chat)
  • Ready to run.
  • Add your own logic and handler methods. core.telegram.org manual is your friend.

Why AutoGram?

The name implies automated-telegram. I needed a framework that is easy and intuitive to work with.

Usage1: Functional

from autogram import Autogram
from autogram.config import Start

#-- handle private dm
@Autogram.add('message')
def message(bot, update):
    print('message:', update)

#-- handle callback queries
@Autogram.add('callback_query')
def callback_query(bot, update):
    print('callback_query:', update)

#***************************** <start>
@Start(config_file='web-auto.json')
def main(config):
    bot = Autogram(config)
    bot.run() # every call fetches updates, and updates internal offset
#-- </start>

Usage2: OOP

import time
from loguru import logger
from threading import Event
from autogram import Autogram, Start

# --
class ExampleBot(Autogram):
    def __init__(self, config):
        super().__init__(config)

    def run(self):
        """Override super().run implementation"""
        super().run() # initializes bot info, abstractmethod
        while not self.stop:
            offset = self.data('offset')
            for rep in self.poll(offset=offset).json()['result']:
              self.data('offset', rep.pop('update_id') + 1)
              with self.register['lock']:
                if handler := self.register['handlers'].get(list(rep.keys())[-1]):
                  handler(self, self, rep)
            time.sleep(5)

    @Autogram.add('message')
    def message(self, bot: Autogram, update):
        logger.debug(update['message']['text'])
        chat_id = update['message']['chat']['id']
        keyb = [[{'text': 'The armpit', 'callback_data': 'tickled'}]]
        data = {
            'reply_markup': bot.getInlineKeyboardMarkup(keyb)
        }
        bot.sendMessage(chat_id, 'Tickle me!', **data)

    # --
    @Autogram.add('callback_query')
    def callback_query(self, bot: Autogram, update):
        callback_id = update['callback_query']['id']
        bot.answerCallbackQuery(callback_id, 'Ha-ha-ha')

#***************************** <start>
@Start()
def main(config):
    bot = ExampleBot(config)
    bot.run()
# ************ </start>

If you have a url-endpoint, call bot.setWebhook(url), then run some sort of webserver in bot.run.

Project TODOs

  • Add webhook example.
  • Extend coverage of the API.

footnotes

  • Invalid TELEGRAM_TOKEN return 404 through bot.getMe()
  • Thread safety is not guaranteed for calls from multiple threads to: bot.data, bot.settings. Prefferably, handle the bot in a single thread, as handlers use these methods to persist data.
  • Don't run multiple bots with the same TOKEN as this will cause update problems
  • Sending unescaped special characters when using MarkdownV2 will return HTTP400
  • Have fun with whatever you're building ;)

Metadata

Release files for autogram 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autogram 4.0.0
File Size Uploaded
autogram-4.0.0.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for autogram 4.0.0
File Interpreter ABI Platform
autogram-4.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.8 kB

Release files / autogram-4.0.0.tar.gz

Download URL autogram-4.0.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2ff218967d43e59953dfe4a531f0412c95202febdb858deb91ed042b19b43938
BLAKE2b-256 checksum
How to use checksums
82acd390a1bb07f7cee6e3cad192f1758793661865f6773d411ffe5207d3daff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.13.1

Release files / autogram-4.0.0-py3-none-any.whl

Download URL autogram-4.0.0-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2474899590fd010ed8200279d43b80b53158e3fa3179de230da02f5984d8d0c8
BLAKE2b-256 checksum
How to use checksums
407f940e405620829c664fe9817f0a0c2c11096270d4204e4b85fe901d77bd98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.13.1

Release history Release notifications | RSS feed

This release

4.0.0 This release

2 release files

3.7.0

2 release files

3.6.1

2 release files

3.6.0

2 release files

3.5.0

2 release files

3.4.4

2 release files

3.4.3

2 release files

3.4.2

2 release files

3.4.1

2 release files

3.4.0

2 release files

3.3.2

2 release files

3.0.0

2 release files

2.0.3

2 release files

1.1.1

2 release files

1.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page