R
R
RoLOQ2021-09-22 20:05:53
Python
RoLOQ, 2021-09-22 20:05:53

How to send the name of the person who pressed the button in the telegram group?

Here is my cut code:

import telebot
import requests
from bs4 import BeautifulSoup
from telebot import types

bot = telebot.TeleBot('KEY')
d = {"some1": "ссылка1",
 "some2": "ссылка2",  "some3": "ссылка3", "some4":"ссылка4"}

@bot.message_handler(commands=['info'])
def info(message):
  full_page = requests.get(d["some4"])
  soup = BeautifulSoup(full_page.content, 'html.parser', from_encoding='UTF-8')
  convert = soup.findAll("div", {"class": "some_class"})
  markup = types.InlineKeyboardMarkup()
  markup.add(types.InlineKeyboardButton('button1', callback_data='some1'),
    types.InlineKeyboardButton('button2', callback_data='some2'),
    types.InlineKeyboardButton('button3', callback_data='some3'),)
  msg = []
  for i in convert:
    msg.append(i.text)
  bot.send_message(message.chat.id, "\n".join(msg), parse_mode='html', reply_markup=markup)

@bot.callback_query_handler(func=lambda call: True)
def button_do(call):
  full_page = d[call.data]
  site = requests.get(full_page)
  soup = BeautifulSoup(site.content, 'html.parser', from_encoding='UTF-8')
  convert = soup.findAll("div", {"class": "some_class"})
  msg = [] # тут нужно вставить то что я описал ниже
  for i in convert:
    msg.append(i.text)
  bot.send_message(call.message.chat.id, "\n".join(msg), parse_mode='html')

Where msg in "def button_do" is needed, so that when the button is pressed, who pressed it
, if you write "call.message.from_user.username", then it will return the username of the bot, not the user,
but if you write call.message.chat. username , then everything will be fine until you press the button in the group,
then the error will be TypeError: sequence item 0: expected str instance, NoneType found

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-09-22
@RoLOQ

call.from_user.username
At least print(call)do

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question