English | فارسی
An interface using telegram API, via "python-telegram-bot" library to add, find and share pastes.
here are required libraries listed in requirements.txt:
- python-telegram-bot
- python-dotenv
- peewee
install the requirements:
pip install -r requirements.txt
create a
.envfile (alongside themain.pyfile), with this content:TOKEN="..." WEBHOOK="..."set
TOKENto your bot token. you can get the token from@botfatherin telegram.if you need to use webhook, set
WEBHOOKto the url. otherwise, don't change it (WEBHOOK="").fire!
python3 main.py
you may use something like
tmuxif you're not using webhook.note: this program uses
sqlite3to save users, chats and pastes. you can change it fromsrc/model/__init__.py. check peewee doc.
there are many files here; however, you just need to check 2 parts:
src/handlerthis folder contains any handler that you need. each handler has a folder in this form:
src/handler/<NAME>. the handler will be written insrc/handler/<NAME>/__init__.py. in this file, you should make a function namedcreator(model).modelis the imported object ofsrc/model. this function receives this parameter, then only returns the handler. you can make a lot of this folders to write your handlers in them.for example, we imagine a handler to handle
/startcommand. you will make a folder and a file likesrc/handler/my_start_handler/__init__.py. then, you'll open this file, and write something like this:fromtelegram.extimportCommandHandler, CallbackContextfromtelegramimportUpdatedefyour_function(update: Update, context: CallbackContext): update.message.reply_text("hello!") defcreator(model): returnCommandHandler("start", your_function)
however, you may want to use an attribute from
src/model, likeHELLO_TEXT. I suggest you to make a function to pass model. this function is written atsrc/handler/functions.py. so, you can use it like this:fromtelegram.extimportCommandHandler, CallbackContextfromtelegramimportUpdatefrom ..functionimportpass_model_todefyour_function(update: Update, context: CallbackContext, model): update.message.reply_text(model.HELLO_TEXT) defcreator(model): returnCommandHandler("start", pass_model_to(your_function, model))
if you have some functions/classes that you want to use them in many handlers, you can put them in
src/handler/functions.py, then, import them.when your handler is completed, you should import its
creatorinsrc/handler/__init__.py, and add it toCREATORStuple. for example, if you want to add thatmy_start_handler, OK? so, add this line tosrc/handler/__init__.py:frommy_start_handlerimportcreatorasmy_start_handler_creator
as you can see, when you add
<NAME>handler, it's better to change itscreatorname to<NAME>_creator(from <NAME> import creator as <NAME>_creator).then, add it to theCREATORStuple.imagine that you have 3 hanlders:
hanlder1(src/hanlder/hanlder1)hanlder2(src/hanlder/hanlder2)hanlder3(src/hanlder/hanlder3)
then, you should write
src/hanlder/__init__.pylike this:fromhandler1importcreatorashandler1_creatorfromhandler2importcreatorashandler2_creatorfromhandler3importcreatorashandler3_creator# from your_new_handler import creaotr as your_new_hanlder_creatorCREATORS= ( handler1_creator, handler2_creator, handler3_creator, # your_new_hanlder_creator, ) # don't change it, don't remove it, don't see it, and don't think about it at all!defget_handlers(model): forcreatorinCREATORES: yieldcreator(model)
note: if you don't add it, then the handler won't be added to the
dispatcher. if your handler is not executed, maybe that's because you didn't add it here!src/modelthis part provides any data about languages, database, etc. for now, it contains the database part, and language part. it is imported and passed to the handlers creator (
modelparameter atcreator(model), remember?). for example, if you defineEXAMPLE = 123atsrc/model/__init__.py, you can use it inmodelparameter asmodel.EXAMPLE.