-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update sample application and readme files
- Loading branch information
Showing
11 changed files
with
156 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Example: Java Application Based Telegram Bot | ||
|
||
*Read this in other languages: [English](README.md), [简体中文](README.zh-cn.md).* | ||
|
||
This is a simple demonstration about how to build an echo bot which will response "Hello Telegram!" every time when message received. | ||
|
||
You can read the source code [here](src/main/java/io/sgr/telegram/bot/examples/hello/HelloTelegramBot.java), it's very easy to understand. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# 示例:基于 Java 命令行的 Telegram 机器人 | ||
|
||
*用其他语言阅读: [English](README.md), [简体中文](README.zh-cn.md).* | ||
|
||
这里我们展示了如何构建一个简单的机器人,它会在收到消息之后回复:"Hello Telegram!"。 | ||
|
||
你可以在[这里](src/main/java/io/sgr/telegram/bot/examples/hello/HelloTelegramBot.java)读到源代码,非常简单。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Example: Spring Boot Based Telegram Bot (Webhook) | ||
|
||
*Read this in other languages: [English](README.md), [简体中文](README.zh-cn.md).* | ||
|
||
This is a simple demonstration about how to build an echo bot which will response "Hello Telegram!" every time when message received. | ||
|
||
Basically it uses a [Controller](src/main/java/io/sgr/telegram/bot/examples/spring/webhook/UpdateController.java) class to listen to updates from Telegram server, it's very easy to understand. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# 示例:基于 Spring Boot 的 Telegram 机器人(Webhook) | ||
|
||
*用其他语言阅读: [English](README.md), [简体中文](README.zh-cn.md).* | ||
|
||
这里我们展示了如何构建一个简单的机器人,它会在收到消息之后回复:"Hello Telegram!"。 | ||
|
||
本质上它是使用一个[Controller](src/main/java/io/sgr/telegram/bot/examples/spring/webhook/UpdateController.java)类监听来自 Telegram 服务器的消息并做出相应,非常简单。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...g-webhook/src/main/java/io/sgr/telegram/bot/examples/spring/webhook/UpdateController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2017-2020 SgrAlpha | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package io.sgr.telegram.bot.examples.spring.webhook; | ||
|
||
import static java.util.Objects.isNull; | ||
|
||
import io.sgr.telegram.bot.api.BotApi; | ||
import io.sgr.telegram.bot.api.exceptions.ApiCallException; | ||
import io.sgr.telegram.bot.api.models.Update; | ||
import io.sgr.telegram.bot.api.models.http.ApiErrorResponse; | ||
import io.sgr.telegram.bot.api.models.http.SendMessagePayload; | ||
import io.sgr.telegram.bot.engine.BotUpdateProcessor; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
@RestController | ||
public class UpdateController implements BotUpdateProcessor { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(UpdateController.class); | ||
|
||
private final String botApiToken; | ||
private final BotApi botApi; | ||
|
||
public UpdateController(@Value("${bot.apiToken}") final String botApiToken) { | ||
this.botApiToken = botApiToken; | ||
this.botApi = BotApi.newBuilder(botApiToken).setLogger(LOGGER).build(); | ||
} | ||
|
||
@PostMapping(path = "/update/{apiToken}") | ||
@ResponseBody | ||
public ResponseEntity<?> receiveUpdate(@PathVariable("apiToken") final String botAndApiToken, @RequestBody final Update update) { | ||
if (!this.botApiToken.equalsIgnoreCase(botAndApiToken)) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); | ||
} | ||
if (handleUpdate(update)) { | ||
return ResponseEntity.ok().build(); | ||
} | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
|
||
@Override | ||
public boolean handleUpdate(@Nonnull final Update update) { | ||
LOGGER.info(update.toJson()); | ||
if (isNull(update.getMessage())) { | ||
return true; | ||
} | ||
final SendMessagePayload payload = new SendMessagePayload(update.getMessage().getChat().getId(), "Hello Telegram!"); | ||
botApi.sendMessage(payload) // Send response message asynchronously without blocking next incoming update. | ||
.exceptionally(e -> { | ||
// Something wrong when sending message, you might want to at least log it. | ||
final Throwable cause = e.getCause(); | ||
if (cause instanceof ApiCallException) { | ||
final String message = ((ApiCallException) cause).getErrorResponse() | ||
.flatMap(ApiErrorResponse::getDescription) | ||
.orElse(cause.getMessage()); | ||
LOGGER.error(message, e); | ||
} else { | ||
LOGGER.error(e.getMessage(), e); | ||
} | ||
return null; // Return null because no message been sent. | ||
}) | ||
.thenAccept(message -> { | ||
// Nullable. Do anything you want with sent message here, or ignore it directly. | ||
}); | ||
return true; | ||
} | ||
} |
63 changes: 0 additions & 63 deletions
63
...g-webhook/src/main/java/io/sgr/telegram/bot/examples/webhook/spring/UpdateController.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters