【问题标题】:how to change api call destination based on user input?(python)如何根据用户输入更改 api 调用目的地?(python)
【发布时间】:2019-01-28 08:06:35
【问题描述】:

我有以下 API 调用点:

iostCall = "https://min-api.cryptocompare.com/data/price?fsym=IOST&tsyms=USD,JPY,EUR"

我想在 (IOST) 连接来自我的电报用户的输入。

这是我目前写的,但我必须对每次调用 api 进行硬编码。

def test (bot, update, args):
    params = {
        'fsym' : fsym
        }
    testCall = "https://min-api.cryptocompare.com/data/price"
    testJson = requests.get(testCall, params=params)
    testOut = testJson
    update.message.reply_text(testOut)

这就是将命令发送到电报 api 的内容。基本上,我的脚本一直等到用户调用 /test(输入硬币名称)......例如:/test btc

    dp.add_handler(CommandHandler("test", test, pass_args=True))

发送给我的用户。

【问题讨论】:

  • currencies = input('enter your currencies (separated by space)').split()

标签: python python-3.x bots telegram


【解决方案1】:

您可以做的是,传递一个新参数,该参数将具有该值。这样您就可以每次更改参数,而不是硬编码。例如:

def iost(bot, update, fsym, tsym):
    params = {
      'fsym' : fsym,
      'tsyms' : tsym
    }

    iostCall = "https://min-api.cryptocompare.com/data/price"
    iostCallJson = requests.get(iostCall, params=params).json()
    iostOut = iostCallJson 
    update.message.reply_text(iostOut)

iost(bot= bot, update= update, fsym='IOST', tsyms='USD,JPY,EUR')

【讨论】:

  • 我的用户使用电报聊天的输入定义 fsym=。 return self.callback(dispatcher.bot, update, **optional_args) TypeError: test() missing 1 required positional argument: 'fsym' 是我得到的错误
  • @toBePythonNinja 你能发布你更新的代码作为编辑吗?
  • 我改变了一些东西,使它成为我想要做的很多检查。我将添加一个新的编辑
  • @toBePythonNinja 是 args 变量吗?
  • 是的,在这种情况下,args 会随着用户输入而变化。 def test 是我的用户 /test 时调用的函数(然后他们将 args 放在此处以类似于 api 信息中的硬币,例如 iost 是 api 中的 orgianl 端点)我希望我的用户输入来编辑此端点调用。
猜你喜欢
  • 2019-05-03
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 2014-01-21
  • 2017-05-15
相关资源
最近更新 更多