R
R
Revs12021-10-24 11:46:02
Python
Revs1, 2021-10-24 11:46:02

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

3 answer(s)
J
jerwright, 2021-10-24
@Revs1

@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)
    #ниже действия, если бы ссылка была в тексте сообщения

V
Vindicar, 2021-10-24
@Vindicar

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."

P
pozner88, 2021-10-24
@pozner88

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:
    #если нет ссылки

As for me the easiest way, according to the idea should work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question