开发者社区> 问答> 正文

如何教电报机器人连接到我的应用程序+ Java + SpringBoot的其余api?

我有一个可以正常工作的数据库应用程序。我有一个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?

展开
收起
垚tutu 2019-12-12 09:46:45 1636 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载