mirror of
https://github.com/miloszowi/everyone-mention-telegram-bot.git
synced 2025-05-20 17:24:06 +00:00
Added inline query handler, updated CHANGELOG, added group name validator
This commit is contained in:
parent
88e8e13ff4
commit
d05d0c0904
@ -1,6 +1,13 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [UNRELEASED] - 07.10.2021
|
||||||
|
### Added
|
||||||
|
- Inline Query for join/leave/everyone
|
||||||
|
- Validator class for group name
|
||||||
|
|
||||||
|
### Updated
|
||||||
|
- start command content
|
||||||
## [0.1.0] - 06.10.2021
|
## [0.1.0] - 06.10.2021
|
||||||
### Features
|
### Features
|
||||||
- `/join` command
|
- `/join` command
|
||||||
|
@ -6,7 +6,7 @@ from telegram.ext.dispatcher import Dispatcher
|
|||||||
from logger import Logger
|
from logger import Logger
|
||||||
from config.credentials import BOT_TOKEN, PORT, WEBHOOK_URL
|
from config.credentials import BOT_TOKEN, PORT, WEBHOOK_URL
|
||||||
from bot.handler import (groupsHandler, joinHandler, mentionHandler, leaveHandler,
|
from bot.handler import (groupsHandler, joinHandler, mentionHandler, leaveHandler,
|
||||||
silentMentionHandler, startHandler)
|
silentMentionHandler, startHandler, inlineQueryHandler)
|
||||||
from bot.handler.abstractHandler import AbstractHandler
|
from bot.handler.abstractHandler import AbstractHandler
|
||||||
|
|
||||||
|
|
||||||
|
46
src/bot/handler/inlineQueryHandler.py
Normal file
46
src/bot/handler/inlineQueryHandler.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
from bot.handler.abstractHandler import AbstractHandler
|
||||||
|
from entity.group import Group
|
||||||
|
from telegram import InlineQueryResultArticle
|
||||||
|
from telegram.ext.callbackcontext import CallbackContext
|
||||||
|
from telegram.ext.commandhandler import CommandHandler
|
||||||
|
from telegram.ext.inlinequeryhandler import \
|
||||||
|
InlineQueryHandler as CoreInlineQueryHandler
|
||||||
|
from telegram.inline.inputtextmessagecontent import InputTextMessageContent
|
||||||
|
from telegram.update import Update
|
||||||
|
|
||||||
|
|
||||||
|
class InlineQueryHandler(AbstractHandler):
|
||||||
|
bot_handler: CommandHandler
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.bot_handler = CoreInlineQueryHandler(self.handle)
|
||||||
|
|
||||||
|
def handle(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
group_display = update.inline_query.query or Group.default_name
|
||||||
|
group = '' if group_display == Group.default_name else group_display
|
||||||
|
|
||||||
|
results = [
|
||||||
|
InlineQueryResultArticle(
|
||||||
|
id='everyone',
|
||||||
|
title='MENTION',
|
||||||
|
description=f'Mention members in group "{group_display}"',
|
||||||
|
input_message_content=InputTextMessageContent(f'/everyone {group}')
|
||||||
|
),
|
||||||
|
InlineQueryResultArticle(
|
||||||
|
id='join',
|
||||||
|
title='JOIN',
|
||||||
|
description=f'Joins group "{group_display}"',
|
||||||
|
input_message_content=InputTextMessageContent(f'/join {group}')
|
||||||
|
),
|
||||||
|
InlineQueryResultArticle(
|
||||||
|
id='leave',
|
||||||
|
title='LEAVE',
|
||||||
|
description=f'Leaves group "{group_display}"',
|
||||||
|
input_message_content=InputTextMessageContent(f'/leave {group}')
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
update.inline_query.answer(results, cache_time=4800)
|
||||||
|
|
||||||
|
def get_bot_handler(self) -> CoreInlineQueryHandler:
|
||||||
|
return self.bot_handler
|
@ -9,6 +9,8 @@ from exception.invalidArgumentException import InvalidArgumentException
|
|||||||
from telegram.ext.callbackcontext import CallbackContext
|
from telegram.ext.callbackcontext import CallbackContext
|
||||||
from telegram.update import Update
|
from telegram.update import Update
|
||||||
|
|
||||||
|
from validator.groupNameValidator import GroupNameValidator
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class MessageData():
|
class MessageData():
|
||||||
@ -24,16 +26,11 @@ class MessageData():
|
|||||||
|
|
||||||
if context.args and context.args[0] and include_group:
|
if context.args and context.args[0] and include_group:
|
||||||
group_name = str(context.args[0]).lower()
|
group_name = str(context.args[0]).lower()
|
||||||
if not re.match(r"^[A-Za-z]+$", group_name):
|
|
||||||
raise InvalidArgumentException(re.escape('Group name must contain only letters.'))
|
|
||||||
|
|
||||||
if group_name == Group.default_name:
|
GroupNameValidator.validate(group_name)
|
||||||
raise InvalidArgumentException(re.escape(f'Group can not be `{Group.default_name}`.'))
|
|
||||||
|
|
||||||
if len(group_name) > 20:
|
if group_name is not Group.default_name:
|
||||||
raise InvalidArgumentException(re.escape(f'Group name length can not be greater than 20.'))
|
chat_id += f'~{group_name}'
|
||||||
|
|
||||||
chat_id += f'~{group_name}'
|
|
||||||
|
|
||||||
|
|
||||||
user_id = str(update.effective_user.id)
|
user_id = str(update.effective_user.id)
|
||||||
|
@ -29,4 +29,7 @@ To see all available groups use:
|
|||||||
|
|
||||||
To display all members in a group:
|
To display all members in a group:
|
||||||
`/silent <group-name>`
|
`/silent <group-name>`
|
||||||
|
|
||||||
|
You can also try to tag me @everyone\_mention\_bot and then enter group name
|
||||||
|
Possible results will be displayed
|
||||||
"""
|
"""
|
16
src/validator/groupNameValidator.py
Normal file
16
src/validator/groupNameValidator.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
from exception.invalidArgumentException import InvalidArgumentException
|
||||||
|
|
||||||
|
|
||||||
|
class GroupNameValidator:
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def validate(group: str) -> None:
|
||||||
|
group = group.lower()
|
||||||
|
|
||||||
|
if len(group) > 0 and not re.match(r"^[A-Za-z]+$", group):
|
||||||
|
raise InvalidArgumentException(re.escape('Group name must contain only letters.'))
|
||||||
|
|
||||||
|
if len(group) > 20:
|
||||||
|
raise InvalidArgumentException(re.escape(f'Group name length can not be greater than 20.'))
|
Loading…
x
Reference in New Issue
Block a user