Quickstart

Basic Example

To get started with telegram.py, we’ll go over a quick example line-by-line.

 1import logging
 2
 3import telegrampy
 4from telegrampy.ext import commands
 5
 6logging.basicConfig(level=logging.INFO, format="(%(asctime)s) %(levelname)s %(message)s", datefmt="%m/%d/%y - %H:%M:%S %Z")
 7logger = logging.getLogger("telegrampy")
 8
 9bot = commands.Bot("token here")
10
11@bot.command()
12async def hi(ctx):
13    await ctx.send("Hello")
14
15bot.run()

Line number(s)

Description

Lines 1-4

Import logging, telegrampy, and the telegram.py commands extension

Lines 6-7

Configure basic logging to get updates in the console

Line 9

Create a commands.Bot instance with a token (get a token from BotFather)

Line 11-12

Define and add a command to our bot called ‘hi’

Line 13

Send a greeting message to the chat where the command was invoked

Line 15

Start the bot (connects to Telegram and starts polling)

More Examples

If you’d like to see more examples, take a look at the examples folder on GitHub.