-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Messaggi di benvenuto più articolati #42
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"it": ["Benvenuto USER nel nostro gruppo ^-^","Ciao USER!","Salve USER!"], | ||
"en": ["Welcome USER to our group ^-^","Hello USER!","Howdy USER!"], | ||
"readme": "https://t.me/c/1095167198/67194", | ||
"utils": [ | ||
"Se hai idee o suggerimenti, condividili con noi. Siamo aperti a nuove prospettive!", | ||
"Non esitare a fare domande se hai bisogno di aiuto.", | ||
"Speriamo che ti possa sentire come a casa tua.", | ||
"Ricorda di rispettare le opinioni degli altri membri, anche se non sempre concordi. La diversità ci arricchisce." | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from os import getenv | ||
from telegram.ext import MessageHandler, Updater, Filters | ||
from module.commands.welcome import send_welcome | ||
|
||
def main() -> None: | ||
TOKEN = getenv("TOKEN") | ||
updater = Updater(TOKEN, use_context=True) | ||
dp = updater.dispatcher | ||
|
||
dp.add_handler(MessageHandler(Filters.status_update.new_chat_members, send_welcome)) | ||
|
||
updater.start_polling() | ||
updater.idle() | ||
|
||
if __name__ == '__main__': | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from telegram import Update, User | ||
from telegram.ext import CallbackContext | ||
from module.shared import welcome | ||
from random import randrange | ||
|
||
|
||
def get_new_user_name(user: User) -> str: | ||
return f"@{user['username']}" if user['username'] is not None else user['first_name'] | ||
|
||
|
||
def generate_welcome(new_member: User) -> str: | ||
new_member_username = get_new_user_name(new_member) | ||
|
||
match new_member["language_code"]: | ||
case "en": | ||
lan_code = "en" | ||
case "it" | _ : | ||
lan_code = "it" | ||
|
||
wlc_mess_list = welcome[lan_code] | ||
|
||
return wlc_mess_list[randrange(0, len(wlc_mess_list))].replace("USER", new_member_username) | ||
|
||
|
||
def send_welcome(update: Update, _: CallbackContext) -> None: | ||
if update.message.new_chat_members: | ||
for new_member in update.message.new_chat_members: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Il controllo dell'esistenza di elementi diventa ridondante, infatti un foreach eventuale su una lista vuota non avrebbe tempo di calcolo aggiuntivo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scusami ma non ho capito benissimo. La miglior scelta è non eseguire il for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In questo caso conviene eseguirlo a prescindere, infatti il costo del for su una lista vuota è praticamente identico al controllare se esiste effettivamente qualcosa su cui iterare ed eventualmente iterare. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah ceeerto, ha perfettamente senso. Sono d'accordo |
||
if not new_member.is_bot: | ||
handle_welcome(update, new_member) | ||
|
||
|
||
def handle_welcome(update: Update, new_member: User) -> None: | ||
welcome_msg = f'{generate_welcome(new_member)}\n' + \ | ||
f"- Dai un'occhiata al [README]({welcome['readme']})\n" + \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. L'uso di
|
||
f"- {welcome['utils'][randrange(0, len(welcome['utils']))]}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Facendo riferimento al commento su There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E come potrebbe diventare "randomico" il messaggio se appena ne leggo uno casuale dal file (la prima volta), esso viene localizzato e ripreso in una futura chiamata della funzione? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Errore mio, intendevo memorizzare la lingua di riferimento in modo da essere passata all'interno di There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quindi l'idea sarebbe creare una sorta di: {
"it" : {
"benvenuti" : ["...", "..." ],
"utils" : ["...", "...", "..." ]
}
"readme" : "https://..."
}
Ho capito bene? Ed eventualmente il There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Esattamente c: |
||
update.message.reply_markdown(welcome_msg, disable_web_page_preview=True) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from json import load | ||
|
||
with open("data/welcome.json", "r") as f: | ||
welcome = load(f) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
python-telegram-bot == 20.0a0 | ||
python-telegram-bot==13.8.1 |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Al fine di mantenere il supporto alla doppia lingua (e ulteriori future) penso possa essere utile trasformare le liste contenute in
it
eden
in dizionari e incorporare all'interno di questi nuovi dizionariutils
in entrambe le lingue