telebot (pyTelegramBotAPI) 2 part

Hello! Today we will continue to study pyTelegramBotAPI.

Pour some tea ☕️ and off we go.

Buttons under the message

import telebot
from telebot import types
token='наш токен'
bot=telebot.TeleBot(token)
@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.chat.id,'Привет')
@bot.message_handler(commands=['button'])
def button_message(message):
    markup=types.InlineKeyboardMarkup()
    button=types.InlineKeyboardButton('Страница моего автора:',url="https://habr.com/ru/users/lubaznatel/posts/")
    button2=types.InlineKeyboardButton('Канал моего автора:',url="https://t.me/joinchat/HwfSdcgwrU1mYWNi")
    markup.add(button,button2)
    bot.send_message(message.chat.id,'Подпишитесь на канал моего автора',reply_markup=markup)
bot.infinity_polling(interval=0, timeout=0)

We will use the code from the previous article, but in the bot.infinity_polling brackets we will write interval = 0, timeout = 0.

Now, in the button function, we will send a message under which there will be buttons.

To do this, we need to create a variable in which the keyboard will be stored.

And the buttons themselves are given a diagram just below.

types.InlineKeyboardMarkup

()

This is the keyboard itself

types.InlineKeyboardButton

(‘Text on the button’, url = “link”)

The button itself

Now we need to add buttons to the keyboard

Let’s write markup.add (button, button2)

Now we need to send a message under which these buttons will be, write bot.send_message (message.chat.id, ‘message text’, reply_markup = markup)

Here’s an example:

Now let’s make the bot check if the user is subscribed to the channel.

Checking your channel subscription

Now we will figure out how to do it in order to check if the user is subscribed to the channel.

We will do this with getChatMember

Now we will learn a new kind of handler that determines the action when one of the buttons is pressed.

We write:

@bot.callback_query_handler(func=lambda call: True)

then we will create a function called callback_inline

def callback_inline(call):

now, before starting to write the function, we need to make the third button

Returning to the function with buttons

And we write there:

button3=types.InlineKeyboardButton('Я подписан',callback_data="user")

And add our third button to the keyboard

Now when this button is pressed, it returns “user”

Back to the callback_inline function

in it we write:

if call.data == "user":
	try:
		a = bot.get_chat_member(chat_id='@lubaznatelhabr', user_id=call.from_user.id)
		if not a.status == 'left':
			bot.answer_callback_query(call.id, text="Ты прошёл проверку", show_alert=True)
    else:
			bot.answer_callback_query(call.id, text="Ты не прошёл проверку", show_alert=True)
  except:
      bot.answer_callback_query(call.id, text="Ты не прошёл проверку", show_alert=True)

I explain, we write “If the returned date = user then try: the variable a stores data about whether the user is in our channel or not, if the status is not left then: the bot will show a warning that you have passed the check”. Then I hope you yourself will understand. Now our bot checks whether we are subscribed to the specified channel or not.

What we got:

And if you are not subscribed to the channel, he will say that “You have not passed the verification.”

Let’s create a variable abcd which will be False

Let’s create it before the variable a

In the check in which the person is subscribed to the channel, write abcd = True.

now after checking the subscription to the channel we write

if abcd:
	@bot.message_handler(commands=['wow'])
  def wow_message(message):
  	bot.send_message(message.chat.id,'wow')

Now the “wow” command works for us only after subscribing to the channel

We now know even more about bots in Python.

This is the end of the article. If you want to learn something else then write in the comments what exactly. And then I will do part 3.

I will ask you to subscribe to me. And I have my own channel in telegram.

Bye.

Bonus

bot.send_photo

(message.chat.id, open (“filename.format”, ‘rb’))

Function for sending a photo

bot.send_video

(message.chat.id, open (“file name.format”, ‘rb’))

bot.send_document

(message.chat.id, open (“filename.format”, ‘rb’))

Channel-Click on me

I’m in telegram- @ ukuleleproger

All code:

import telebot
from telebot import types
token='Наш токен'
bot=telebot.TeleBot(token)
@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.chat.id,'Привет')
@bot.message_handler(commands=['button'])
def button_message(message):
    markup=types.InlineKeyboardMarkup()
    button=types.InlineKeyboardButton('Страница моего автора:',url="https://habr.com/ru/users/lubaznatel/posts/")
    button2=types.InlineKeyboardButton('Канал моего автора:',url="https://t.me/lubaznatelhabr")
    button3=types.InlineKeyboardButton('Я подписан',callback_data="user")
    markup.add(button,button2,button3)
    bot.send_message(message.chat.id,'Подпишитесь на канал моего автора',reply_markup=markup)
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    if call.data == "user":
        try:
            abcd=False
            a = bot.get_chat_member(chat_id='@lubaznatelhabr', user_id=call.from_user.id)
            if not a.status == 'left':
                bot.answer_callback_query(call.id, text="Ты прошёл проверку", show_alert=True)
                abcd=True
            else:
                bot.answer_callback_query(call.id, text="Ты не прошёл проверку", show_alert=True)
        except:
            bot.answer_callback_query(call.id, text="Ты не прошёл проверку", show_alert=True)
        if abcd:
            @bot.message_handler(commands=['wow'])
            def wow_message(message):
                bot.send_message(message.chat.id,'wow')
bot.infinity_polling(interval=0, timeout=0)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *