如何在 Telegram Bot 中使用 setWebhook 后的 getUpdates API

How to use getUpdates after setWebhook in Telegram Bot API

我为我的电报机器人使用 setWebhook,现在我需要使用 getUpdates。我已经阅读了文档,他们说我只能使用一种方法。

问题是,我在控制台中有:

{"ok":false,"error_code":409,"description":"Error: Conflict: another webhook is active"}

所以问题是,如何UNSET webhook 并使用getUpdates?

如 Telegram bot api 文档中所述,您只需将空字符串传递给 url 参数。

> base_url = 'https://api.telegram.org/bot' + TOKEN + '/'
> data = {"url": ""}
> requests.post(base_url + 'setWebhook', data=data)

在浏览器中发送以下请求:

https://api.telegram.org/bot655390656:bhFS50...ff3zO4/setwebhook

你可以直接调用方法

deleteWebhook()

https://core.telegram.org/bots/api#deletewebhook

例如使用 telepot

import telepot
bot = telepot.Bot('YOUR_AUTHORIZATION_TOKEN')
bot.deleteWebhook()

我为这份工作写了一个小的 rake 任务

require 'net/https'

namespace :telegram_custom do
  desc "Deactives webhook - this is needed to enable polling in development"
  task deactivate_webhook: :environment do
    token = "YOUR_BOT_TOKEN"
    base_url = "https://api.telegram.org/bot#{token}/setwebhook"
    uri = URI.parse(base_url)

    res = Net::HTTP.get URI(base_url)
    puts res
  end
end

如果您将令牌存储在凭据中,您可以通过以下方式获取它们:token = Rails.application.credentials.telegram[:bot]