tgbarebot/README.md

97 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

2022-01-03 17:58:26 +01:00
# Minimalistic, single-file, telegram bot library.
# No dependencies, tested with python3.10
2019-08-30 18:02:58 +02:00
2021-12-24 09:39:23 +01:00
### NOTE: EXTREMELY BAREBONE
2019-08-30 18:02:58 +02:00
#### and probably, only useful in text-based bots only
2022-01-03 17:58:26 +01:00
#### This lib only does extremely basic api calls.
#### Implementing everything else is up to you.
2021-01-01 22:45:58 +01:00
2022-01-03 17:58:26 +01:00
installation: import the file into your main, or use a git submodule.
I don't want this on pip. (yet?)
2019-08-30 18:02:58 +02:00
## Sample main:
```
import json
2022-01-03 17:58:26 +01:00
import logging
from bot import Bot
2019-08-30 18:02:58 +02:00
2022-01-03 17:58:26 +01:00
logging.basicConfig(filename='debug.log', level=logging.DEBUG)
2021-12-24 09:39:23 +01:00
2022-01-03 17:58:26 +01:00
def testcommand(bot : Bot, text, chat):
print("wew")
return bot.get_botinfo()
2021-12-24 09:39:23 +01:00
# defining some sample commands
def start(bot, text, chat):
2022-01-03 17:58:26 +01:00
return "HI " + chat["from"]["first_name"] + "!!!!\nIt's a pleasure to work with yOU!!!!!"
2019-08-30 18:02:58 +02:00
2021-12-24 09:39:23 +01:00
def debug(bot, text, chat):
2019-08-30 18:02:58 +02:00
out = "text sent = " + text + "\n"
out += "chat info = \n\n" + str(chat)
return out
2021-12-24 09:39:23 +01:00
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 ""
2019-08-30 18:02:58 +02:00
def main():
print("starting bot lmao")
try:
2021-12-24 09:39:23 +01:00
token = json.load(open( "token.json" , 'r'))["TOKEN"]
2019-08-30 18:02:58 +02:00
except FileNotFoundError:
print("Warning: missing token.json. Critical fucking failure IDIOT!!!")
exit(0)
2022-01-03 17:58:26 +01:00
commands = {"/start": start, "/debug": debug, "/remind": remind_Jack, "/test" : testcommand}
2019-08-30 18:02:58 +02:00
2022-01-03 17:58:26 +01:00
b = Bot(token, commands) # feed the bot with your token and a dict of commands.
2019-08-30 18:02:58 +02:00
b.poll()
if __name__ == "__main__":
main()
2019-08-30 18:02:58 +02:00
```
##### 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
2019-08-30 18:09:43 +02:00
- `fallback` = a function that returns a string to handle non-command updates, a.k.a generic messages.
2019-08-30 18:02:58 +02:00
- `setup` = an arbitrary function to be executed when the bot starts.
To start the bot, use the method `poll()`
2021-12-24 09:39:23 +01:00
##### Defining user functions:
2019-08-30 18:02:58 +02:00
2021-12-24 09:39:23 +01:00
Every user function must take three arguments, `bot`, `text` and `chat`:
2019-08-30 18:02:58 +02:00
2021-12-24 09:39:23 +01:00
- `bot` is the bot object itself. This argument is needed if you ever want to call some bot functions within a command.
2019-08-30 18:02:58 +02:00
- `text` is a string containing the whole text of the message that triggered the command.
2021-12-24 09:39:23 +01:00
- `chat` contains the whole message update in form of a dictionary.
2019-08-30 18:02:58 +02:00
2021-12-24 09:39:23 +01:00
`text` is implemented as a shorthand, while all kinds of info related to the update is contained into the `chat` parameter (even text itself).
2019-08-30 18:02:58 +02:00
2021-12-24 09:39:23 +01:00
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.
2019-08-30 18:02:58 +02:00
2022-01-03 17:58:26 +01:00
##### 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.
2021-12-24 09:39:23 +01:00
### TODO:
2019-08-30 18:02:58 +02:00
2021-12-24 09:39:23 +01:00
- 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)