A bare-bones python library to make telegram bots.
Go to file
Raphy 977491a3f6 fixing yet another dumb typo 2022-02-03 07:02:59 +01:00
README.md Formatted code, minor changes, changed the way the telegram api is encoded (to make implementing the rest of the API easier) 2022-02-01 10:14:12 +01:00
bot.py fixing yet another dumb typo 2022-02-03 07:02:59 +01:00

README.md

Minimalistic, single-file, telegram bot library.

No dependencies, tested with python3.10

NOTE: EXTREMELY BAREBONE

and probably, only useful in text-based bots only

This lib only does extremely basic api calls.

Implementing everything else is up to you.

installation: import the file into your main, or use a git submodule. I don't want this on pip. (yet?)

Sample main:

import json
import logging
from bot import Bot

logging.basicConfig(filename='debug.log', level=logging.DEBUG)

def testcommand(bot : Bot, text, chat):
    print("wew")
    return bot.get_botinfo()
# defining some sample commands
def start(bot, text, chat):
    return "HI " + chat["from"]["first_name"] + "!!!!\nIt's a pleasure to work with yOU!!!!!"

def debug(bot, text, chat):
    out = "text sent = " + text + "\n"
    out += "chat info = \n\n" + str(chat)
    return out

def remind_Jack(bot, text, chat):
    
    bot.send("Jack, you're an idiot! haha", 111111) # supposedly, 111111 is jack's telegram id. This is just an example.

    return ""

def main():
    print("starting bot lmao")

    try:
        token = json.load(open( "token.json" , 'r'))["TOKEN"] 
    except FileNotFoundError:
        print("Warning: missing token.json. Critical fucking failure IDIOT!!!")
        exit(0)

    commands = {"/start": start, "/debug": debug, "/remind": remind_Jack, "/test" : testcommand}
    
    b = Bot(token, commands) # feed the bot with your token and a dict of commands.
    b.poll()

if __name__ == "__main__":
    main()

The bot class constructor takes the following arguments:
positional arguments
  • token = your bot's token, in string format.
  • commands = a dictionary containing the various command hooks, and a function that returns a string, like such: {"command1":firstcommand, "commands2":secondcommand}

in the example, "/start" and "/debug" are provided. More on how to create a function below.

non-positional, optional arguments
  • fallback = a function that returns a string to handle non-command updates, a.k.a generic messages.
  • setup = an arbitrary function to be executed when the bot starts.

To start the bot, use the method poll()

Defining user functions:

Every user function must take three arguments, bot, text and chat:

  • bot is the bot object itself. This argument is needed if you ever want to call some bot functions within a command.
  • text is a string containing the whole text of the message that triggered the command.
  • chat contains the whole message update in form of a dictionary.

text is implemented as a shorthand, while all kinds of info related to the update is contained into the chat parameter (even text itself).

Sample functions are presented in the sample code above. To understand better chat's structure, consult the telegram api, or use the debug command presented in the sample code.

Logging:

The library makes use of python's standard logging lib. See the example main to see a simple logging configuration. It will log pretty much anything and will supposedly never crash.

TODO:

  • Add more functions to Bot (ideally the entire telegram api)
  • Document the functions better
  • Explain how to hack state into this thing (there's a way to do it, but it's fairly painful)