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
.env
file (alongside themain.py
file), with this content:TOKEN="..." WEBHOOK="..."
set
TOKEN
to your bot token. you can get the token from@botfather
in telegram.if you need to use webhook, set
WEBHOOK
to the url. otherwise, don't change it (WEBHOOK=""
). -
fire!
python3 main.py
you may use something like
tmux
if you're not using webhook.note: this program uses
sqlite3
to 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/handler
this 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)
.model
is 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
/start
command. 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:from telegram.ext import CommandHandler, CallbackContext from telegram import Update def your_function(update: Update, context: CallbackContext): update.message.reply_text("hello!") def creator(model): return CommandHandler("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:from telegram.ext import CommandHandler, CallbackContext from telegram import Update from ..function import pass_model_to def your_function(update: Update, context: CallbackContext, model): update.message.reply_text(model.HELLO_TEXT) def creator(model): return CommandHandler("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
creator
insrc/handler/__init__.py
, and add it toCREATORS
tuple. for example, if you want to add thatmy_start_handler
, OK? so, add this line tosrc/handler/__init__.py
:from my_start_handler import creator as my_start_handler_creator
as you can see, when you add
<NAME>
handler, it's better to change itscreator
name to<NAME>_creator
(from <NAME> import creator as <NAME>_creator
).then, add it to theCREATORS
tuple.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__.py
like this:from handler1 import creator as handler1_creator from handler2 import creator as handler2_creator from handler3 import creator as handler3_creator # from your_new_handler import creaotr as your_new_hanlder_creator CREATORS = ( 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! def get_handlers(model): for creator in CREATORES: yield creator(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/model
this 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 (
model
parameter atcreator(model)
, remember?). for example, if you defineEXAMPLE = 123
atsrc/model/__init__.py
, you can use it inmodel
parameter asmodel.EXAMPLE
.