A
A
Artem Kiryanov2022-04-04 10:40:42
Bots
Artem Kiryanov, 2022-04-04 10:40:42

How to add https to telebot link?

I use the telebot library for bots.
The problem is, I can't process the link. The scenario is this, the user enters a link, but it happens that the link can be short, without https, www, for example - test.ru. I need to do such processing so that the bot itself adds https and www, and if the user specifies a full link, i.e. from https://www.test.ru , then everything is OK and go to the next step.

When the user presses - start, the function is called:

@bot.callback_query_handler(func=lambda message: True)
def send_anytext(message):
    chat_id = message.message.chat.id
    if message.data == 'utm_links':
       msg = bot.send_message(chat_id, 'Введите адрес сайта в формате https://domain.ru/\n\nНапример, https://yandex.ru/', parse_mode='HTML')
    bot.register_next_step_handler(msg, get_utm_source)


The next step, perhaps on the get_utm_source function, is processing
def get_utm_source(message):
    global get_utm_source;
    get_utm_source = message.text;
    
    chat_id = message.chat.id
    msg = bot.send_message(chat_id, "Введите сообщение")
    bot.register_next_step_handler(msg, get_utm_medium)


I made a separate one like this:
def convert(url):
    if url.startswith('http://www.'):
        return 'http://' + url[len('http://www.'):]
    if url.startswith('www.'):
        return 'http://' + url[len('www.'):]
    if not url.startswith('http://'):
        return 'http://' + url
    return url


But something I can not understand how to embed it in the bot code.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2022-04-04
@Aveyloff

You need to make an envelope like this

def convert(url):
    if not 'https://' in url:
        return 'https://' + url
    else:
        return url

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question