Skip to main content

Telegram Bot API

Project description

Telegrapy

Telegrapy is a package for easy building of bots using the Telegram Bot API.


How to start (for now)

  1. Clone the repo
$ cd PATH_OF_YOUR_CHOICE
$ git clone https://github.com/sudogene/telegrapy.git
$ cd telegrapy
  1. Test your bot's account using the following code
from telegrapy import Bot

token = 'YOUR_TOKEN'
bot = Bot(token)

print(bot.get_me())
  1. Set bot to run in a loop and keep program alive
bot.run()
print('Running...')

while True:
  time.sleep(3)

When the bot runs, it indefinitely checks for message updates and handles them.

Quick example on an echo bot can be found in example.py.


Telegram Objects

Telegram Objects in telegrapy are object-oriented implementation of JSON-objects in the Telegram API. Each object has a unique identifier (ID) and its corresponding JSON. Hence, any object can return its own JSON format by calling the .json attribute.

User

Based on Telegram User. These are typically senders of Message.

Chat

Based on Telegram Chat. They are subclassed into PrivateChat, GroupChat, and SupergroupChat. As of now, there are not many differences in terms of parsing messages from these chat types. Chat IDs are required for bots to send messages to.

Message

Based on Telegram Message. The main bulk of data necessary for bot interactions. Contains information of the date time, sender, chat, command, and text.


Creating Bot Commands

Bot commands are special text/phrases recognized by bots as commands, and trigger bots to call functions defined by the user. All messages received by the bot are parsed and handled by the Handler class which is attached to the Bot upon creation. You can create your handler functions using the function signature

def function_name(msg: telegrapy.Message)

where all functions MUST take in the Message object. A code example:

def echo(msg):
  chat_id = msg.chat_id
  text = msg.text
  bot.send_message(chat_id, text)

Defined handler functions can then be added to the bot's handler:

# get the handelr
handler = bot.handler

# add the function
handler.add_command('echo', echo)

Adding of commands to the handler requires two arguments; the string command and the function.


Bot Methods

Not to be confused with bot commands. Bot methods are GET and POST HTTP methods based on Telegram Available Methods. As of now, the following methods have been implemented in this library:

Method telegrapy.Bot function
getMe get_me
sendMessage send_message
sendPhoto send_photo
sendAudio send_audio
sendDocument send_document
sendVideo send_video
sendVoice send_voice
sendDice send_dice

Optional Arguments for Bot Methods

Some methods support additional arguments specific to use case. An example is reply_to_message_id which allows the bot's message to reply to a specific message. This requires the Message ID, which is conveniently available in the Message input for all handler functions.

# Direct reply to the sender's message
def echo(msg):
  chat_id = msg.chat_id
  text = msg.text
  bot.send_message(chat_id, text, reply_to_message_id=msg.id)

bot.handler.add_command('echo', echo)

... More to be added

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegrapy-0.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telegrapy-0.1-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

Details for the file telegrapy-0.1.tar.gz.

File metadata

  • Download URL: telegrapy-0.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.0

File hashes

Hashes for telegrapy-0.1.tar.gz
Algorithm Hash digest
SHA256 1a2630f1184700fe8b792cc7dec3f9ac97fdd7aa3e6965cfb893482212e1c605
MD5 66925ed561e2a5cdd6547edf8c8de03e
BLAKE2b-256 148697a9cadcb320ea08d4d79d1a4d0957019a4408237dfa467c1529fb4a7067

See more details on using hashes here.

File details

Details for the file telegrapy-0.1-py3-none-any.whl.

File metadata

  • Download URL: telegrapy-0.1-py3-none-any.whl
  • Upload date:
  • Size: 19.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.0

File hashes

Hashes for telegrapy-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 01f224139e7eb8edac6c345f88765388e78c686cae18e5112b2511f2e3634af8
MD5 59efd0766dc12a8870a7f25db6687b64
BLAKE2b-256 365a8629c38d5af434bd16e789184f98b2d0afd8653eaa06a6fe6ff88bd7ab93

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page