-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1302 from coder2020official/master
Some useful filters
- Loading branch information
Showing
6 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import telebot | ||
from telebot import custom_filters | ||
|
||
bot = telebot.TeleBot('token') | ||
|
||
|
||
# Chat id can be private or supergroups. | ||
@bot.message_handler(chat_id=[12345678], commands=['admin']) # chat_id checks id corresponds to your list or not. | ||
def admin_rep(message): | ||
bot.send_message(message.chat.id, "You are allowed to use this command.") | ||
|
||
@bot.message_handler(commands=['admin']) | ||
def not_admin(message): | ||
bot.send_message(message.chat.id, "You are not allowed to use this command") | ||
|
||
|
||
# Do not forget to register | ||
bot.add_custom_filter(custom_filters.UserFilter()) | ||
|
||
|
||
bot.polling(non_stop=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import telebot | ||
from telebot import custom_filters | ||
|
||
bot = telebot.TeleBot('TOKEN') | ||
|
||
|
||
# Check if message starts with @admin tag | ||
@bot.message_handler(text_startswith="@admin") | ||
def start_filter(message): | ||
bot.send_message(message.chat.id, "Looks like you are calling admin, wait...") | ||
|
||
# Check if text is hi or hello | ||
@bot.message_handler(text=['hi','hello']) | ||
def text_filter(message): | ||
bot.send_message(message.chat.id, "Hi, {name}!".format(name=message.from_user.first_name)) | ||
|
||
# Do not forget to register filters | ||
bot.add_custom_filter(custom_filters.TextFilter()) | ||
bot.add_custom_filter(custom_filters.TextStarts()) | ||
|
||
bot.polling(non_stop=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from telebot import util | ||
|
||
|
||
|
||
class TextFilter(util.AdvancedCustomFilter): | ||
""" | ||
Filter to check Text message. | ||
key: text | ||
Example: | ||
@bot.message_handler(text=['account']) | ||
""" | ||
|
||
key = 'text' | ||
|
||
def check(self, message, text): | ||
if type(text) is list:return message.text in text | ||
else: return text == message.text | ||
|
||
class TextContains(util.AdvancedCustomFilter): | ||
""" | ||
Filter to check Text message. | ||
key: text | ||
Example: | ||
# Will respond if any message.text contains word 'account' | ||
@bot.message_handler(text_contains=['account']) | ||
""" | ||
|
||
key = 'text_contains' | ||
|
||
def check(self, message, text): | ||
return text in message.text | ||
|
||
class UserFilter(util.AdvancedCustomFilter): | ||
""" | ||
Check whether chat_id corresponds to given chat_id. | ||
Example: | ||
@bot.message_handler(chat_id=[99999]) | ||
""" | ||
|
||
key = 'chat_id' | ||
def check(self, message, text): | ||
return message.chat.id in text | ||
|
||
|
||
class TextStarts(util.AdvancedCustomFilter): | ||
""" | ||
Filter to check whether message starts with some text. | ||
Example: | ||
# Will work if message.text starts with 'Sir'. | ||
@bot.message_handler(text_startswith='Sir') | ||
""" | ||
|
||
key = 'text_startswith' | ||
def check(self, message, text): | ||
return message.text.startswith(text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -485,3 +485,5 @@ def check(self, message, text): | |
Perform a check. | ||
""" | ||
pass | ||
|
||
|