如何接收viber REST API回调(使用java、spring)?
我正在尝试使用java/spring制作viber机器人(公共帐户)。我成功地为bot设置了webhook(通过将其部署到heroku并发出post请求)
{
"url":"https://my.host.com"
}
使用邮递员)现在我正在尝试从用户那里接收消息。问题是,我是使用spring的新手,实际上不知道如何接收任何viber事件
这是我的代码(只是检查我是否能收到viber回调):
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping
public String request(@RequestHeader("X-Viber-Content-Signature") String serverSideSignature) {
System.out.println("Here we go!");
System.out.println(serverSideSignature);
return "Hello world!";
}
}
然而,这是行不通的。如果我使用必要的serverSideSignature
(文档中的示例)使用邮递员发出请求,例如
{
"event":"message",
"timestamp":1457764197627,
"message_token":4912661846655238145,
"sender":{
"id":"01234567890A=",
"name":"John McClane",
"avatar":"http://avatar.example.com",
"country":"UK",
"language":"en",
"api_version":1
},
"message":{
"type":"text",
"text":"a message to the service",
"media":"http://example.com",
"location":{
"lat":50.76891,
"lon":6.11499
},
"tracking_data":"tracking data"
}
}
我可以看到控制台输出。但如果我向bot发送消息(甚至不从日志中获取任何信息),它就不起作用。我想问题是我不太明白viber是如何将任何信息发送到我的webhook的,但也许有人能为我解释一下?非常感谢
# 1 楼答案
@RequestMapping默认使用GET方法,Viber使用POST方法向webhook发送回调。尝试将@RequestMapping更改为@PostMapping,当然,您应该确保您的webhook设置正确,并且您的URL具有来自可信CA的有效官方SSL证书