Quickstart

Basic Example

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import logging

import telegrampy
from telegrampy.ext import commands

logging.basicConfig(level=logging.INFO, format="(%(asctime)s) %(levelname)s %(message)s", datefmt="%m/%d/%y - %H:%M:%S %Z")
logger = logging.getLogger("telegrampy")

bot = commands.Bot("token here")

@bot.command()
async def hi(ctx):
    await ctx.send("Hello")

bot.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.