B
B
Bumrud2018-05-23 06:16:20
Python
Bumrud, 2018-05-23 06:16:20

A simple telegram bot?

Good morning. Help understand and implement the bot. Let's divide the question into two parts.
the bot sends pictures, the pictures are divided into groups, cats and dogs. The client chooses what he will receive. Through what methods it is better to implement buttons. As I understand it, 1 script receives data from the client and saves it. how the bot determines which pictures it sends to whom and does not duplicate sending to one client. It is desirable that all this functionality be password-protected on the client.
Can you describe in short or not in short, how can this be implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
spikejke, 2018-05-23
@spikejke

1 - the bot sends a request to select image content

@bot.message_handler(commands=['start']
def question(message):
    markup = types.ReplyKeyboardMarkup()
    buttondogs = types.KeyboardButton('dogs')
    buttoncats = types.KeyboardButton('cats')
    markup.add(buttondogs, buttoncats)
    bot.reply_to(message, "Animals", reply_markup=markup)

2 - Based on the user's choice, we display available pictures
@bot.message_handler(func=lambda message: message in ['dogs', 'cats']
def photo_animals(message):
    if message.text == 'dogs':
        bot.send_photo(message.chat.id, dog_photo_file_id, reply_to_message_id=message.message_id)
    if message.text == 'cats'
        bot.send_photo(message.chat.id, cat_photo_file_id, reply_to_message_id=message.message_id)

..._photo_file_id = unique photo ID You can get its value using this function:
@bot.message_handler(content_types=['photo'])
def photo_field_id(message):
        print(message.photo[0].file_id)

We can send photos using the built-in open function , but the telegram developers themselves recommend using file_id Pass a file_id as String to send a photo that exists on the Telegram servers (recommended)
This is the simplest implementation of the bot, how you implement it depends on your imagination.
Useful links:
https://core.telegram.org/bots/api
https://github.com/eternnoir/pyTelegramBotAPI

S
Simon Osipov, 2018-05-23
@SimonOsipov

Take a look at the Python Telegram Bot .
There are examples, you can peep the implementation of the simplest bots.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question