如何在电报机器人中添加inlineKeyboard的URL

发布于 2025-01-25 21:24:21 字数 2540 浏览 2 评论 0原文

我想创建一个按钮,当我单击它时,将在电报中将文件发送给我。

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!")

不要工作inlinekeyboard,只是发送给我链接下载,也不要显示我的按钮

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()

我运行程序,并且文件发送给我,但它不问我128或320,它会自动发送文件为什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰つ倒转 2025-02-01 21:24:21

在键盘部分中,您应该为每个按钮具有唯一的回调数据。例如:

keyboard = [
              [
                  InlineKeyboardButton('128KB', callback_data='dl-128'),
                  InlineKeyboardButton('320kb', callback_data='dl-320'),
              ]
          ]

然后,您应该创建一个函数来处理回调查询:

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

之后,您应该使用首选模式创建一个回调处理程序。对于此示例,将是:

button_handler = CallbackQueryHandler(button, pattern='dl-')
dispathcer.add_handler(button_handler)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文