我有一个可以正常工作的数据库应用程序。我有一个Rest API,可以连接到该API并获取数据或进行编辑。我创建了一个电报自动程序,但是它只能回复消息。
@Component
public class Bot extends TelegramLongPollingBot {
Logger logger = LoggerFactory.getLogger(Bot.class);
private static String ERR_MESS_SEND = "Failed to send a message: ";
private static String ERR_MESS_SEND_USER ="Failed to send a message form method sendTextMessaged(): ";
@Value("${bot.username}")
String botUsername;
@Value("${bot.token}")
String botToken;
final private GreetingSendService greetingSendService;
private ManageCommands manageCommands;
private ButtonService buttonService;
@Autowired
public Bot(GreetingSendService greetingSendService,
ManageCommands manageCommands,
ButtonService buttonService) {
this.greetingSendService = greetingSendService;
this.manageCommands = manageCommands;
this.buttonService = buttonService;
}
/**
* For receiving messages.
* the method is processing messages that was received.
* isValidMessage - the check : is a message and is it a text?
* inMessage - is getting an object of input message
* outMessage - is making an object of output message
* outMessage.setChatId(chatIdFromInMess) - Set the chat ID, where you want
* to send a message-this is the chat, where this message came from
*
* @param update - It is a message from user.
*/
@Override
public void onUpdateReceived(Update update) {
// parseReceivedMessage(update);
sendMess(update);
}
..............
您可以在这里传递单词,但不能带空格。作为响应,我得到json。这还不够。
/*Here is obtained the stream data that was gotten from rest-api*/
private String obtainStreamDataFromResponse(String message){
// URL url = new URL("http://api.openweathermap.org/data/2.5/weather?q=" + message + "&units=metric&appid=6fff53a641b9b9a799cfd6b079f5cd4e");
URL url = null;
try {
url = new URL("http://localhost:8080/api/city/" + message);
} catch (MalformedURLException e) {
logger.info(ERR_GET_API, e);
}
Scanner in = null;
try {
in = new Scanner((InputStream) url.getContent());
} catch (IOException e) {
logger.info( ERR_PARSE_RESPONSE, e);
}
StringBuilder result = new StringBuilder();
while (in.hasNext()) {
result.append(in.nextLine());
}
return result.toString();
}
这是其余的api
@GetMapping("name/{town:[A-Za-z-.]+]}")
public NameDto getDataAboutName(@PathVariable String name) {
return targetReadService.readEntry(name);
}
@PostMapping("create")
public ResponseEntity<Object> create(@RequestBody NameDto dto) {
this.createTargetService.createEntry(dto);
return ResponseEntity.ok(HttpStatus.OK);
}
以及如何配置我的应用程序,以便它不仅允许Bot请求数据,还允许它删除和编辑并连接到RSET api?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。