Package: origamibot
Library for creating bots for telegram withPython.OrigamiBotaims to make development of Telegram bots as easy and flexible as possible.InstallationOrigamibot is published inPyPI, so it can be installed with one simple command:pip install origamibotBasic conceptsOrigamiBotclass is thing that will get updates form the server and dispatch them to yourcommand holdersandevent listeners.Command Holderis a custom class that you create that exposes its methods as commands for bot
command holder can be attached to bot usingbot.add_commands(your_command_holder_class())Event listeneris a class that inherits fromorigamibot.util.Listenerand performs some actions on certain events(when itson_<something>methods are called fromOrigamiBot). Event listener can be added to bot withbot.add_listener(your_listener_object)Usage exampleHere goes a simple example of a bot:fromsysimportargvfromtimeimportsleepfromorigamibotimportOrigamiBotasBotfromorigamibot.listenerimportListenerclassBotsCommands:def__init__(self,bot:Bot):# Can initialize however you likeself.bot=botdefstart(self,message):# /start commandself.bot.send_message(message.chat.id,'Hello user!\nThis is an example bot.')defecho(self,message,value:str):# /echo [value: str] commandself.bot.send_message(message.chat.id,value)defadd(self,message,a:float,b:float):# /add [a: float] [b: float]self.bot.send_message(message.chat.id,str(a+b))def_not_a_command(self):# This method not considered a commandprint('I am not a command')classMessageListener(Listener):# Event listener must inherit Listenerdef__init__(self,bot):self.bot=botself.m_count=0defon_message(self,message):# called on every messageself.m_count+=1print(f'Total messages:{self.m_count}')defon_command_failure(self,message,err=None):# When command failsiferrisNone:self.bot.send_message(message.chat.id,'Command failed to bind arguments!')else:self.bot.send_message(message.chat.id,f'Error in command:\n{err}')if__name__=='__main__':token=(argv[1]iflen(argv)>1elseinput('Enter bot token: '))bot=Bot(token)# Create instance of OrigamiBot class# Add an event listenerbot.add_listener(MessageListener(bot))# Add a command holderbot.add_commands(BotsCommands(bot))# We can add as many command holders# and event listeners as we likebot.start()# start bot's threadswhileTrue:sleep(1)# Can also do some useful work i main thread# Like autoposting to channels for exampleCommands are added as methods of an object(be it class or instance of it), if their names don't start with_which makes it possible to also contain some utility functions inside command container.For the command to be called two conditions must be met:command name must match with method namecommand's arguments must match signature of a methodMethod signature supports any number of arguments with simple typing(str,int,float,bool) or without a typing(in this case all arguments are strings by default), as well as variable number of arguments*args. More complex types(as lists, tuples, custom object classes) are not supported, as bot does not know how to parse them, and I don't want to enforce my own parsing algorithm, but bot will still attempt to convert it likecls(argument), but a correct result is not guaranteed.Booleanvalues are considered True if their string representation is in{'True', 'true', '1'}, and False if in{'False', 'false', '0'}