Skip to content

Commit

Permalink
feat: add /start command (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
domenicoblanco authored Mar 3, 2024
1 parent 183d09c commit 8d87c11
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from os import getenv
from telegram import Update, User
from telegram.ext import ApplicationBuilder, MessageHandler, CallbackContext, filters
from telegram.ext import ApplicationBuilder, MessageHandler, CallbackContext, CommandHandler, filters
from json import load
from random import randrange

Expand Down Expand Up @@ -36,13 +36,27 @@ async def send_welcome(update: Update, _: CallbackContext) -> None:
f'- {welcome_file[lan_code]["utils"][randrange(0, len(welcome_file[lan_code]["utils"]))]}'
await update.message.reply_text(f'{welcome_msg}')


async def command_start(update: Update, _) -> None:
lan_code = 'it' if update.message.from_user.language_code == 'it' else 'en'
reply = ''

with open("src/welcome.json", "r") as f:
reply = load(f)[lan_code]['start_message']

await update.message.reply_text(reply)


def main() -> None:
token = getenv("QDBotToken")
app = ApplicationBuilder().token(token).build()
app.add_handler(MessageHandler(
filters.ChatType.GROUPS & filters.StatusUpdate.NEW_CHAT_MEMBERS,
send_welcome
))
app.add_handler(CommandHandler(
"start", command_start
))
app.run_polling()


Expand Down
6 changes: 4 additions & 2 deletions src/welcome.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"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"
]
],
"start_message": "Ciao! Sono il Welcome-Bot, aggiungimi a un gruppo per dare il benvenuto ai nuovi membri!"
},
"en": {
"sentences" : ["Welcome USER to our group ^-^","Hello USER!","Howdy USER!"],
Expand All @@ -15,7 +16,8 @@
"Don't hesitate to ask questions if you need help",
"We hope you can feel at home",
"Remember to respect other members' opinions, even if you don't always agree. Diversity enriches us."
]
],
"start_message": "Hi! I'm the Welcome-Bot, add me to a group to welcome your new users!"
},
"readme" : "https://t.me/c/1095167198/67194/67418"
}
18 changes: 18 additions & 0 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ def get_wel(lan_code: str) -> str:
'mock_obj': [main, Application],
'mock_func': ['getenv', 'run_polling'],
'mock_ret': ['123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11', True]
},
{
'func': main.command_start,
'expected_res': None,
'arg': (main.Update(0, message=Message(0, from_user=possible_users[4], chat=Chat(0, type='GROUP'), date=datetime.now())), None),
'mock_obj': [Message],
'mock_func': ['reply_text'],
'mock_ret': [True],
'is_async': True
},
{
'func': main.command_start,
'expected_res': None,
'arg': (main.Update(0, message=Message(0, from_user=possible_users[5], chat=Chat(0, type='GROUP'), date=datetime.now())), None),
'mock_obj': [Message],
'mock_func': ['reply_text'],
'mock_ret': [True],
'is_async': True
}
]

Expand Down

0 comments on commit 8d87c11

Please sign in to comment.