'How Can I add URL for Inlinekeyboard in Telegram Bot
I want to create a button that when I click on it will send the file to me in the telegram.
from config import TOKEN
from telegram import MessageEntity, ReplyKeyboardMarkup, Update
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater
from telegram.ext import CallbackContext
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters
from telegram.ext import CallbackQueryHandler
import logging
import urllib.request , json
updater = Updater(token= TOKEN, use_context= True)
dispathcer = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
download_url_link = 'https://saavn.me/songs?link='
home_page = 'https://saavn.me/home'
def readjson(url):
req = urllib.request.urlopen(url)
return json.loads(req.read().decode())
def start(update: Update, context: CallbackContext):
text= "Welcome @{username}".format(username = update.effective_user.username)
update.message.reply_text(text)
def download(update: Update, context: CallbackContext):
x = update.message.parse_entities(types = MessageEntity.URL)
msg = update.message.reply_text("Working on it...")
for i in x:
try:
rjson = read_json(end_point_link + x[i])
name = rjson["results"]['name']
download_link = rjson["results"]["downloadUrl"][4]["link"]
quality = rjson["results"]["downloadUrl"][4]["quality"]
msg.delete()
dl = update.message.reply_document(download_link, caption="Here is {} and quality is {}".format(name, quality))
keyboard = [
[
InlineKeyboardButton('128KB', callback_data=dl),
InlineKeyboardButton('320kb', callback_data=dl),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text("Please choose:",
reply_markup=reply_markup)
except:
continue
if 'error' in rjson:
continue
return
msg.edit_text("I can't fetch from that url. try again!")
dont work inlinekeyboard and just send for me link download and dont show my buttons
start_handler = CommandHandler('start', start)
download_hundler = MessageHandler(Filters.entity(MessageEntity.URL), download)
button_handler = CallbackQueryHandler(button)
dispathcer.add_handler(start_handler)
dispathcer.add_handler(download_hundler)
dispathcer.add_handler(button_handler)
updater.start_polling()
I run the program and the file is sent to me but it does not ask me 128 or 320 and it automatically sends the file why?
Solution 1:[1]
In the keyboard section, you should have a unique callback data for each button. for example:
keyboard = [
[
InlineKeyboardButton('128KB', callback_data='dl-128'),
InlineKeyboardButton('320kb', callback_data='dl-320'),
]
]
Then you should create a function to handle callback query:
def button(update: Update, context: CallbackContext):
data = update.callback_query.data
quality = int(data.split('-')[1])
if quality == 128:
# send 128kb file
elif quality == 320:
# send 320kb file
after that, you should create a callback handler with your preferred pattern. For this example it would be:
button_handler = CallbackQueryHandler(button, pattern='dl-')
dispathcer.add_handler(button_handler)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Javad Zahiri |