Telethon/telethon_examples
2019-01-07 15:48:25 +01:00
..
assistant.py New assistant command and check if hastebin is down 2018-11-15 17:54:51 +01:00
gui.py Make disconnect synchronous 2018-10-16 11:56:17 +02:00
interactive_telegram_client.py Update examples to make them easier to run 2018-09-04 11:52:18 +02:00
LICENSE Tidy examples and update licenses 2019-01-07 15:48:25 +01:00
print_messages.py Tidy examples and update licenses 2019-01-07 15:48:25 +01:00
print_updates.py Tidy examples and update licenses 2019-01-07 15:48:25 +01:00
README.md Tidy examples and update licenses 2019-01-07 15:48:25 +01:00
replier.py Tidy examples and update licenses 2019-01-07 15:48:25 +01:00
screenshot-gui.jpg Tidy examples and update licenses 2019-01-07 15:48:25 +01:00

Examples

This folder contains several single-file examples using Telethon.

Requisites

You should have the telethon library installed with pip. Run python3 -m pip install --upgrade telethon --user if you don't have it installed yet (this is the most portable way to install it).

Examples assume you have the following environment variables defined:

You can ignore any of these, and the scripts will prompt you to enter them.

Downloading Examples

You may download all and run any example with by typing in a terminal:

git clone https://github.com/LonamiWebs/Telethon.git
cd Telethon
cd telethon_examples
python3 gui.py

You can also right-click the title of any example and use "Save Link As…" to download only a particular example.

All examples are licensed under the CC0 License, so you can use them as the base for your own code without worrying about copyright.

Available Examples

print_updates.py

  • Usable as a: user and bot.
  • Difficulty: easy.

Trivial example that just prints all the updates Telegram originally sends. Your terminal should support UTF-8, or Python may fail to print some characters on screen.

print_messages.py

  • Usable as a: user and bot.
  • Difficulty: easy.

This example uses the different @client.on syntax to register event handlers, and uses the pattern= variable to filter only some messages.

There are a lot other things you can do, but you should refer to the documentation of events.NewMessage since this is only a simple example.

replier.py

  • Usable as a: user and bot.
  • Difficulty: easy.

This example showcases a third way to add event handlers (using decorators but without the client; you should use the one you prefer) and will also reply to some messages with different reactions, or to your commands.

It also shows how to enable logging, which you should always do, but was not really needed for the previous two trivial examples.

assistant.py

  • Usable as a: bot.
  • Difficulty: medium.

This example is the actual bot account @TelethonianBot running in the official Telethon's chat to help people out. The file is a bit big and assumes some asyncio knowledge, but otherwise is easy to follow.

interactive_telegram_client.py

  • Usable as a: user and bot.
  • Difficulty: medium.

Interactive terminal client that you can use to list your dialogs, send messages, delete them, and download media. The code is a bit long which may make it harder to follow, and requires saving some state in order for downloads to work later.

gui.py

  • Usable as a: user and bot.
  • Difficulty: high.

This is a simple GUI written with tkinter which becomes more complicated when there's a need to use asyncio (although it's only a bit of additional setup). The code to deal with the interface and the commands the GUI supports also complicate the code further and require knowledge and careful reading.

This example is the actual bot account @TelethonianBot running in the official Telethon's chat to help people out. The file is a bit big and assumes some asyncio knowledge, but otherwise is easy to follow.

Screenshot of the tkinter GUI