簡體   English   中英

Java webhook controller 用於 Telegram 機器人

[英]Java webhook controller for a Telegram bot

我創建了一個基本的 spring 引導應用程序(從 heroku 開始的 java-getting-started),添加了一個端點,我已將其配置為我的電報機器人上的 webhook。

但是,端點似乎對 GET 請求做出反應,我希望它是一個 POST,因此有一個要解析的主體

@Controller
@SpringBootApplication
public class Main {

  @RequestMapping(value = "/testendpoint", method = RequestMethod.GET)
  public String testendpoint() {
    // send a response to the bot, this part works fine
  }

我應該如何閱讀電報機器人發送的內容? 它應該是 json ,其中包含諸如message > chat > idmessage > text等字段,PHP 中的等效項適用於此:

$update = json_decode(file_get_contents("php://input"), TRUE)

$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];

來自https://nordicapis.com/how-to-build-your-first-telegram-bot-using-php-in-under-30-minutes/

所以我有點困惑我的 controller 應該如何處理內容..

獲取請求也支持正文。 您需要在 testendpoint() 方法中接受方法參數並使用 @RequestBody 注釋參數,以便底層對象映射器嘗試將您的請求解析為參數類型的 class。 顯然,請求正文的模式應該與請求 class 匹配。

  @RequestMapping(value = "/testendpoint", method = RequestMethod.GET)
  public String testendpoint(@RequestBody MyRequestClass request) {
    // send a response to the bot, this part works fine
  }

此外,您的機器人和端點上的動詞應該匹配。 因此,要么讓他們通過 GET 或通過 POST 交談。 您應該選擇適合交互語義的選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM