Answer the question
In order to leave comments, you need to log in
How to check link in telegram bot?
Hello!
I am writing a telegram bot and I want to check for the text entered from the user so that the link "instagram.com/" is present in this text, if there is no link in the text, then it gives an error, I don’t understand how to implement it.
Answer the question
In order to leave comments, you need to log in
@bot.message_handler(content_types = ['text'])
def testing(message):
needed_item = "instagram.com/"
if needed_item not in message.text.lower():
return bot.reply_to(message, f"В вашем тексте нет {needed_item}", disable_web_page_preview=True)
#ниже действия, если бы ссылка была в тексте сообщения
If you don't know how to search for the presence of a substring in a string (either through the in operator or through the index() method ), or how to use regular expressions , then it's too early for you to start bots . Start with simpler exercises, then return to the topic.
That's jokes aside. These are the basics of the language's standard library. Before you rush to develop something, you need to know them, at least at the level of capabilities, i.e. "I don't remember the name of the method for converting a string to lowercase, but I know for sure that it exists, I'll go look."
You can use the count method. There may be other solutions, but this one came to my mind first.
Using the count method, you can simply make a condition that checks if there are keywords you need. For example with instagram.com/ you can do something like this
link=message.text
if link.count("instagram.com/") == 1:
#дальнейшие действия если ссылка есть
else:
#если нет ссылки
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question