【问题标题】:Telegram API using curl in windows throws error of "message text is empty"在 Windows 中使用 curl 的 Telegram API 引发“消息文本为空”错误
【发布时间】:2021-07-07 15:32:44
【问题描述】:

我正在运行以下命令,但收到以下错误消息,提示文本为空。但它不是。我也尝试过 POST ,它仍然会出现同样的错误。有什么想法吗?

curl.exe -k https://api.telegram.org/bot0000:xxxxx/sendMessage?chat_id=333333&text=yooo

{"ok":false,"error_code":400,"description":"错误请求:消息文本 为空"}'text' 未被识别为内部或外部命令, 可运行的程序或批处理文件。

【问题讨论】:

    标签: windows curl telegram telegram-bot telegram-webhook


    【解决方案1】:

    您是如何让 Curl 在 Windows 中工作的?我的猜测是Pearl Bash hack。好的,这就是我过去发现的。当您将 Curl 与 Windows Powershell 一起使用时,它并不真正知道如何处理内容。 例子: 当您进行 API 调用时,例如“获取所有机器 ID”,您可能会获得 100 个值的列表。通常,您会为所需的名称执行类似“Grep”的操作,然后将“id”值传递给变量并使用它来影响目标。 我发现发生的是“文本”字符串没有作为字符串对象传递,而是某种机器类型对象。简而言之,您最终必须转换数据,然后您必须弄清楚如何搜索它。它变得非常快。

    解决方案: 调用-WebRequest

    虽然这个模块需要一点时间来适应,但当您使用它时,您可以将值作为 Powershell 丰富对象传递,并使用每个人都喜欢的 Powershell 语法。

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.1

    #chat_id variable creation. This might need to be in the header, I don't know this API but wanted to show the example 
    $body = @{ 
                chat_id = 333333
                text = yooo 
             } 
    #Example. Actual code may vary. 
    $message = @{Invoke-RestMethod "https://api.telegram.org/ `
        - Headers @{Authorization = "bot0000 xxxxx"} `
        - Body $body `
        - Method  Get }
    #now we can use $message to see what's available.
    #we can then use $message.whatever to view the responses. 
    

    【讨论】:

    • 嘿Patoshi,我试图'powershellafi'你的代码。可能不是您所需要的,但我认为它可以帮助您浏览该模块。如果您还有其他问题,请告诉我。
    猜你喜欢
    • 2018-07-25
    • 2018-10-05
    • 1970-01-01
    • 2014-08-11
    • 2023-03-23
    • 1970-01-01
    • 2017-09-17
    • 2014-12-28
    • 2016-02-23
    相关资源
    最近更新 更多