Package: tebot
TeBot - Telegram bot library for Python and humansThe goal is to to keep it simple.Demo (sometimes may not work):@demo_tebotHow to installpip3installtebotHow to useStart botIf your project usesneotaskerfromtebotimportTeBot# delay - delay between pollings, must be specifiedmybot=TeBot(delay=2)# obtain token from https://telegram.me/BotFathermybot.set_token('botsecrettoken')# optionally - load previous stateimportjsonwithopen('bot-state.json')asfh:mybot.load(json.loads(fh.read()))# start botmybot.start()If it doesn't:fromneotaskerimporttask_supervisorfromtebotimportTeBottask_supervisor.create_aloop('default',default=True)task_supervisor.start()mybot=TeBot(delay=2)mybot.set_token('botsecrettoken')mybot.start()(refer toneotaskerdocumentation for more info)Stop botmybot.stop()# if your project doesn't use neotaskertask_supervisor.stop()# optionally - save bot statewithopen('bot-state.json','w')asfh:fh.write(json.dumps(mybot.serialize()))Send messages# textmybot.send(text='hello world',chat_id=chat_id)# fileswithopen('image.jpg','rb')asfh:mybot.send(media=fh.read(),chat_id=chat_id)If message is being sent from the handler andchat_idis not specified,
current chat ID is used:@mybot.route(path='/start')defstart(**kwargs):mybot.send('bot started')Download filesdefsomehandler(**kwargs):payload=kwargs.get('payload')if'document'inpayload:try:content=mybot.get_file_content(payload['document'].get('file_id'))# process file contentexcept:# unable to download fileHigh-level API: routesTeBothas flask-style routes, which may be registered either by callingmybot.register_route(fn,path,methods)or with function decorator:# message handler. can be only one, registered to handle all regular messages@mybot.route(methods='message')defmy_message(chat_id,text,**kwargs):# some code# command handler for /start and /help@mybot.route(path=['/start','/help'])defstart(**kwargs):mybot.send('got HELP command')# command and callback query handler@mybot.route(methods='*')defdefault_cmd_handler(path,**kwargs):mybot.send(f'command not implemented:{path}')Route parameterspathcommand path, can be string or list/tuple for multiple commandsmethodscan be either a string or a list/tuple. Valid values are:
"message", "command" (default if no methods specified) and
"query" / "callback_query". If "*" specified, the method is registered for
both commands and callback queriesHandler kwargsThe following kwargs are sent to registered handlers:textmessage text (only for message handler)pathcommand path (e.g. "/select" for "/select * from data")query_stringcommand query string (e.g. "* from data" for the above
example)chat_idcurrent chat idquery_idcallback query id, if handler is executed as a callback query
handlerpayloadfull request payloadmethod"command" or "query" for callback queryHandler return dataIf command is handled, the handler may return nothingIf callback query is handled, the handler may return dict, which is used as a
payload for the callback query answer (e.g. include "url", "show_alert" etc,
see Telegram Bot API for more details)Low-level API: handlersOverride class methods:handle_messagehandle regular messageshandle_commandhandle commands (starting
with '/')handle_queryhandle callback querieson_messageoverride to implement advanced message handlingon_queryoverride to implement advanced callback query handlingBot optionsmybot.timeout=5# set Telegram API timeout (default: 10 sec)mybot.retry_interval=1# if API command fails, re-send it in 1 second# (default: None, don't re-send)Web hooksTo use web hooks, init bot object,but don't start it. Useprocess_update(payload)method to process webhook payloads.TeBot doesn't have own web server module, you may use any available.To register webhook, useset_webhookbot object method (args are the same as
forhttps://core.telegram.org/bots/api#setwebhook)To delete webhook, usedelete_webhookbot object method (no args required).Everything elseRefer to function pydoc for more info.