Package: tinybot
tinybotThis is a small and simple library for quickly building Telegram Bots with Python.It is made to be dynamic and independent of the Telegram Bot API.
It means that whenever a new Telegram API method or callback is added,
there is no need to update this library, all calls are dynamic and you can send whatever you like to Telegram.InstallationIt is published on PyPI so to use it simply install it with pip.$ pip install tinybotExampleActually working little bot (from which this library was originated) can be foundhere.Here is a simple example:importtinybotclassAssistant(tinybot.Bot):version='0.0.1-SNAPSHOT'token='123456789:token-for-debugging'# token is not required when using tinybot.rundefhandle_message(self,data,api):if'@all'indata.text:api.sendMessage(chat_id=data.chat.id,text='@necauqua @other_people..')api.sendMessage(chat_id=data.chat.id,text='[debug] update id '+str(self.update_id))if__name__=='__main__':# just start longpoll loop for debuggingAssistant.launch_longpoll(30)# tinybot.run is a ready-to-use CLI for launching longpoll or webhook and providing the token# tinybot.run(Assistant, 'necauqua assistant bot')UsageYour bot is a class which derives fromtinybot.Botclass.
You can start it with a ready-to-use CLI using this standard constructif__name__=='__main__':tinybot.run(bot_cls,bot_description)All of the bot's logic is defined inside the class deriven fromtinybot.Bot.It have static fieldsname,version,full_nameandtoken.namedefaults to the class name and is the name of your bot. Used infull_nameversionis a version of your bot, it defaults to0.1.0. Used infull_name.full_namedefaults toname/version.
It is used as aUser-Sessionheader when making requests, and asServerheader when
responding to Telegram from a webhook server.tokenis an optional field, because usually it is set from command line when runningtinybot.run.
However, it might be useful for debugging.
It is obviously the token which is used for all requests.Your class should define methods with signarurehandle_xxx(self, data, api)wherexxxis one of the update types foundhere.For example:defhandle_message(self,message,api):pass# ordefhandle_channel_post(self,post,api):passselfparameter can contain some additional data, for now it only contains theupdate_idfield.dataparameter is a dict/list-like recursive structure made with__getattr__'s
and__getitems__'s in which the received Update JSON object is given.
When you try to get a nonexistent field,NoSuchElementExceptionis raised and
then handled properly by thelaunch_xxxmethods caller.apiparameter worls similarly, any method call which looks likeapi.anyMethodCall(**kwargs)would send appropriate request to the Telegram Bot API.When you do such a request, Telegram returns JSON in format{"ok": true, "update": ..data..}or{"ok": false, "description": "..error message.."}.This JSON is unwrapped and if ok is false or non-existent,RequestExceptionis raised
with the description and similarly handled by thelaunch_xxxmethods caller.
Else, the"update"object is wrapped in the same structure as thedataparameter and
is returned from theapimethod call.All of the above means that if you want your bot to do multiple get-and-then-send requests,
you'll want to arrange your code to first get all the data and only after that send your requests.
That way, if any of the data weren't available, any of your mutating requests would not be sent.LicenseIt is licensed under permissive MIT license which means you can use this code in
whatever way possible, as long as you include the LICENSE file (by which you mention my authorship).
It is included in the package so you have to do nothing yourself when simply using this library.