Answer the question
In order to leave comments, you need to log in
How to change nickname in telegram by button?
I am writing a Python script - a bot that will change my nickname using the buttons from the telegram keyboard. There was a problem - the bot changes its nickname only when called under with, and I cannot insert bot handlers under it - then the bot will not accept them. How can this problem be solved? The code:
print("o")
from pyrogram import Client
from pyrogram.raw import functions
import time
import json
from datetime import datetime
print("+")
import telebot
from telebot import types
bot = telebot.TeleBot('токен')
print("bot component setup finished")
config = json.loads(open('config.json').read())
with Client("app", config['API_ID'], config['API_HASH']) as app:
#сама функция смена ника, под with она работает
def ChangeNick(nick):
print('CHANGE NICK TO:', nick)
try:
app.update_profile(first_name="Pyrogram")
except Exception as e:
print(e)
#app.send(functions.account.UpdateProfile(first_name="Test"))
print("nick changed")
@bot.message_handler(content_types=['text'])
def start(message):
print("пришло сообщение")
if message.text == 'ъъъ':
keyboard = types.InlineKeyboardMarkup()
buttons = [
types.InlineKeyboardButton(text='Работаю', callback_data='working'),
types.InlineKeyboardButton(text='Сплю', callback_data='sleeping'),
types.InlineKeyboardButton(text='По умолчанию', callback_data='default')
]
for b in buttons:
keyboard.add(b)
bot.send_message(message.from_user.id, text="?", reply_markup=keyboard)
else:
bot.send_message(message.from_user.id, '?');
@bot.callback_query_handler(func=lambda call: True)
def callback_worker(call):
ChangeNick(config.get("NAMES").get(call.data)+" []") #смена ника по кнопке
bot.polling(none_stop=True, interval=0)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question