简体   繁体   English

如何根据用户输入更改 api 调用目标?(python)

[英]how to change api call destination based on user input?(python)

I have the following API call point:我有以下 API 调用点:

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

I want to concatenate an input from my telegram user at (IOST).我想连接来自(IOST)的电报用户的输入。

this is what I have currently wrote but i have to hard code each call to the api.这是我目前编写的内容,但我必须对每次调用 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)

and this is what sends the command to the telegram api.这就是将命令发送到电报 api 的原因。 basically, my script waits till a user calls /test (inputs a coin name)... for example: /test btc基本上,我的脚本一直等到用户调用 /test(输入硬币名称)...例如:/test btc

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

sends it to my user.发送给我的用户。

What you can do is, pass a new parameter which will have the value.您可以做的是,传递一个具有该值的新参数。 This way you can change the parameter every time, rather than hard coding.这样您就可以每次更改参数,而不是硬编码。 For example:例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM