{"id":345091,"date":"2023-02-07T15:04:50","date_gmt":"2023-02-07T15:04:50","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=345091"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2023-02-07T15:04:50","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=345091","title":{"rendered":"<span>\u041f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 Telegram bot \u043d\u0430 java, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u0443\u0440\u0441 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u0438\u043d\u043e\u0441\u0442\u0440\u0430\u043d\u043d\u044b\u043c \u0432\u0430\u043b\u044e\u0442\u0430\u043c<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u0414\u0430-\u0434\u0430 \u0442\u0435\u043c\u0430 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u0430 \u0438 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0440\u0430\u0445 habr-a \u0435\u0441\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u0440\u043e \u0442\u043e, \u043a\u0430\u043a \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e telegram bota, \u043d\u043e \u044f \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u0440\u0438\u0441\u043a\u043d\u0443 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0443. \u041c\u043e\u0436\u0435\u0442 \u043a\u043e\u043c\u0443-\u0442\u043e \u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430 \u0438 \u043f\u043e\u043b\u0435\u0437\u043d\u0430.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0431\u043e\u0442\u0430 \u043d\u0443\u0436\u0435\u043d \u0430\u043a\u043a\u0430\u0443\u043d\u0442 \u0432 telegram.<\/p>\n<p>\u0418 \u0442\u0430\u043a \u043d\u0430\u0447\u043d\u0435\u043c: \u043d\u0430\u0448 \u0431\u043e\u0442 \u0431\u0443\u0434\u0435\u0442 &#171;\u0445\u043e\u0434\u0438\u0442\u044c&#187; \u043d\u0430 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0431\u0430\u043d\u043a\u0430 \u0438 \u0432\u044b\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u0443\u0440\u0441 \u043a \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u043e\u0439 \u0432\u0430\u043b\u044e\u0442\u0435.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0435\u043a\u0442, \u044f \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u044e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/start.spring.io\/\" rel=\"noopener noreferrer nofollow\">https:\/\/start.spring.io\/<\/a><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/df4\/2d2\/a54\/df42d2a5433e624ff651367b138efe24.png\" width=\"1587\" height=\"939\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/df4\/2d2\/a54\/df42d2a5433e624ff651367b138efe24.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0438 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0432 \u0441\u0440\u0435\u0434\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0439\u0442\u0438 \u0432 telegram \u0438 \u043d\u0430\u0439\u0442\u0438 \u0442\u0430\u043c <a href=\"https:\/\/habr.com\/users\/botfather.\" rel=\"noopener noreferrer nofollow\"><u>@BotFather.<\/u><\/a>  <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/0b9\/9aa\/302\/0b99aa30245e90f82fc7cd2100c3c4bd.png\" width=\"686\" height=\"69\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0b9\/9aa\/302\/0b99aa30245e90f82fc7cd2100c3c4bd.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0438\u0448\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \/newbot \u0438 \u0432\u0432\u043e\u0434\u0438\u043c \u0438\u043c\u044f \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u0431\u043e\u0442\u0430. \u0418\u043c\u044f \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 bot. \u041f\u043e \u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u0438 \u043d\u0430\u0441 \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u043a\u0430\u0442\u044c \u0432 telegram.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7d3\/29b\/5e6\/7d329b5e64de3bb60b35be46ad1e6a2e.png\" width=\"537\" height=\"561\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7d3\/29b\/5e6\/7d329b5e64de3bb60b35be46ad1e6a2e.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e BotFather \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u0430\u043c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043d\u0435\u043c\u0443. \u0414\u0430\u043d\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u043b\u0443\u0447\u0448\u0435 \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u0441\u0435\u043a\u0440\u0435\u0442\u0435 \u0438 \u043d\u0438\u043a\u043e\u043c\u0443 \u043d\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0443\u0437\u043d\u0430\u0432 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u0442\u043e\u043c\u0443 \u0431\u043e\u0442\u0443. \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f \u0435\u0433\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u044e.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0435\u043c \u0432 \u043d\u0430\u0448 \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043b\u044f telegram, lombok \u0438 json. \u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 json \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 jsona, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 API \u0431\u0430\u043d\u043a\u0430.<\/p>\n<pre><code class=\"java\">&lt;dependency> &lt;groupId>org.telegram&lt;\/groupId> &lt;artifactId>telegrambots&lt;\/artifactId> &lt;version>6.5.0&lt;\/version> &lt;\/dependency>  &lt;dependency> &lt;groupId>org.projectlombok&lt;\/groupId> &lt;artifactId>lombok&lt;\/artifactId> &lt;version>1.18.26&lt;\/version> &lt;scope>provided&lt;\/scope> &lt;\/dependency>      &lt;dependency> &lt;groupId>org.json&lt;\/groupId> &lt;artifactId>json&lt;\/artifactId> &lt;version>20220924&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<p>\u0412 application.properties \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0443 \u043a \u0431\u043e\u0442\u0443.<\/p>\n<pre><code class=\"java\">bot.name=OfficialRateOfBYNBot bot.token=5888893679:AAGQWAbS0zCMtnQrTeWrVYz9KHj2nUlyVIw<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 config, \u0438 \u0442\u0430\u043c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 BotConfig. \u041e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u043c\u0435\u043d\u0438 \u0438 \u0442\u043e\u043a\u0435\u043d\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u043e\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u043d\u0435\u043c\u0443.<\/p>\n<pre><code class=\"java\">@Configuration @Data @PropertySource(\"application.properties\") public class BotConfig {     @Value(\"${bot.name}\")     String botName;     @Value(\"${bot.token}\")     String token; }<\/code><\/pre>\n<p> \u0421\u0435\u0439\u0447\u0430\u0441 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u0440\u043e API \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u043a\u0443\u0440\u0441\u0430\u0445 \u0432\u0430\u043b\u044e\u0442. \u042f \u043d\u0430\u0448\u0435\u043b \u0442\u0430\u043a\u043e\u0439 <a href=\"https:\/\/www.nbrb.by\/apihelp\/exrates\" rel=\"noopener noreferrer nofollow\">https:\/\/www.nbrb.by\/apihelp\/exrates<\/a> \u043d\u0430 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u0430\u0439\u0442\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0431\u0430\u043d\u043a\u0430.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/eba\/810\/11a\/eba81011aaaf27ca601e4415d26cdfa8.png\" width=\"1769\" height=\"875\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/eba\/810\/11a\/eba81011aaaf27ca601e4415d26cdfa8.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0442\u043f\u0440\u0430\u0432\u0438\u0432 get \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 API \u0447\u0435\u0440\u0435\u0437 postman \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 json.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/5a2\/c73\/486\/5a2c73486ed3222904088d11cd610b62.png\" width=\"1501\" height=\"773\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5a2\/c73\/486\/5a2c73486ed3222904088d11cd610b62.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u043b\u0435\u0433\u0447\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c json \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 CurrencyModel \u0432\u00a0\u043f\u0430\u043f\u043a\u0435 model<\/p>\n<pre><code class=\"java\">@Data public class CurrencyModel {     Integer cur_ID;     Date date;     String cur_Abbreviation;     Integer cur_Scale;     String cur_Name;     Double cur_OfficialRate; }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 CurrencyService \u0432\u00a0\u043f\u0430\u043f\u043a\u0435 service.<\/p>\n<pre><code class=\"java\">public class CurrencyService {      public static String getCurrencyRate(String message, CurrencyModel model) throws IOException, ParseException {         URL url = new URL(\"https:\/\/www.nbrb.by\/api\/exrates\/rates\/\" + message + \"?parammode=2\");         Scanner scanner = new Scanner((InputStream) url.getContent());         String result = \"\";         while (scanner.hasNext()){             result +=scanner.nextLine();         }         JSONObject object = new JSONObject(result);          model.setCur_ID(object.getInt(\"Cur_ID\"));         model.setDate(new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss\").parse(object.getString(\"Date\")));         model.setCur_Abbreviation(object.getString(\"Cur_Abbreviation\"));         model.setCur_Scale(object.getInt(\"Cur_Scale\"));         model.setCur_Name(object.getString(\"Cur_Name\"));         model.setCur_OfficialRate(object.getDouble(\"Cur_OfficialRate\"));           return \"Official rate of BYN to \" + model.getCur_Abbreviation() + \"\\n\" +                 \"on the date: \" + getFormatDate(model) + \"\\n\" +                 \"is: \" + model.getCur_OfficialRate() + \" BYN per \" + model.getCur_Scale() + \" \" + model.getCur_Abbreviation();      }      private static String getFormatDate(CurrencyModel model) {         return new SimpleDateFormat(\"dd MMM yyyy\").format(model.getDate());     } }<\/code><\/pre>\n<p>\u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u043c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 getCurrencyRate, \u0434\u0435\u043b\u0430\u0435\u043c \u0435\u0433\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a\u00a0\u043d\u0435\u043c\u0443 \u0431\u0435\u0437\u00a0\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. \u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u043c\u044b \u00ab\u0438\u0434\u0435\u043c\u00bb \u043d\u0430\u00a0\u0441\u0430\u0439\u0442 \u041d\u0411 \u0438 \u0442\u0430\u043c \u043f\u043e\u00a0\u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u043e\u0439 \u0432\u0430\u043b\u044e\u0442\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0435\u0451 \u043a\u0443\u0440\u0441 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432\u00a0\u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c CurrencyModel. \u041d\u0430\u00a0\u0432\u044b\u0445\u043e\u0434\u0435 \u0432\u044b\u0434\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435, \u0438\u0437\u00a0\u044d\u0442\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0443\u0436\u0435 \u0432\u00a0\u0442\u0430\u043a\u043e\u043c \u0432\u0438\u0434\u0435, \u0432\u00a0\u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0438\u0445 \u0431\u0443\u0434\u0443\u0442 \u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0430\u0448\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 TelegramBot \u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u0441\u044f \u043e\u0442\u00a0TelegramLongPollingBot.<\/p>\n<pre><code class=\"java\">@Component @AllArgsConstructor public class TelegramBot extends TelegramLongPollingBot {     private final BotConfig botConfig;      @Override     public String getBotUsername() {         return botConfig.getBotName();     }      @Override     public String getBotToken() {         return botConfig.getToken();     }      @Override     public void onUpdateReceived(Update update) {         CurrencyModel currencyModel = new CurrencyModel();         String currency = \"\";          if(update.hasMessage() &amp;&amp; update.getMessage().hasText()){             String messageText = update.getMessage().getText();             long chatId = update.getMessage().getChatId();              switch (messageText){                 case \"\/start\":                     startCommandReceived(chatId, update.getMessage().getChat().getFirstName());                     break;                 default:                     try {                         currency = CurrencyService.getCurrencyRate(messageText, currencyModel);                      } catch (IOException e) {                         sendMessage(chatId, \"We have not found such a currency.\" + \"\\n\" +                                 \"Enter the currency whose official exchange rate\" + \"\\n\" +                                 \"you want to know in relation to BYN.\" + \"\\n\" +                                 \"For example: USD\");                     } catch (ParseException e) {                         throw new RuntimeException(\"Unable to parse date\");                     }                     sendMessage(chatId, currency);             }         }      }      private void startCommandReceived(Long chatId, String name) {         String answer = \"Hi, \" + name + \", nice to meet you!\" + \"\\n\" +                 \"Enter the currency whose official exchange rate\" + \"\\n\" +                 \"you want to know in relation to BYN.\" + \"\\n\" +                 \"For example: USD\";         sendMessage(chatId, answer);     }      private void sendMessage(Long chatId, String textToSend){         SendMessage sendMessage = new SendMessage();         sendMessage.setChatId(String.valueOf(chatId));         sendMessage.setText(textToSend);         try {             execute(sendMessage);         } catch (TelegramApiException e) {          }     } }<\/code><\/pre>\n<p>\u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0432\u043d\u0435\u0434\u0440\u044f\u0435\u043c \u043a\u043b\u0430\u0441\u0441 BotConfig \u0441\u00a0\u043d\u0430\u0448\u0438\u043c\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u043c\u0438 \u0434\u043b\u044f\u00a0\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a\u00a0\u0431\u043e\u0442\u0443. \u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 TelegramLongPollingBot: <\/p>\n<p>getBotUsername()\u00a0\u2014 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0438\u043c\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u043e\u0442\u0430.<\/p>\n<p>getBotToken()\u00a0\u2014 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u043e\u0442\u0430.<\/p>\n<p>onUpdateReceived(Update update)\u00a0\u2014 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438\u00a0\u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c. \u041e\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u00a0\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c update, \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0435\u043a\u0441\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, id \u0447\u0430\u0442\u0430 \u0434\u043b\u044f\u00a0\u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u043e\u0442\u0432\u0435\u0442\u043d\u043e\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430:<\/p>\n<p>startCommandReceived(Long chatId, String name)\u00a0\u2014 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u043d\u0430\u0431\u043e\u0440\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \/start \u0432\u00a0telegram. <\/p>\n<p>sendMessage(Long chatId, String textToSend)\u00a0\u2014 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0447\u0435\u0440\u0435\u0437 id \u0447\u0430\u0442\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432\u00a0telegram.<\/p>\n<p>\u0418 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c BotInitializer \u0432\u00a0\u043f\u0430\u043f\u043a\u0435 config.<\/p>\n<pre><code class=\"java\">@Component public class BotInitializer {     private final TelegramBot telegramBot;     @Autowired     public BotInitializer(TelegramBot telegramBot) {         this.telegramBot = telegramBot;     }      @EventListener({ContextRefreshedEvent.class})     public void init()throws TelegramApiException{         TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);         try{             telegramBotsApi.registerBot(telegramBot);         } catch (TelegramApiException e){          }     } }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0435\u0441\u0441\u0438\u044e \u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0435\u0433\u043e telegram \u0431\u043e\u0442\u0430.<\/p>\n<p>\u041d\u0430\u0448 \u0431\u043e\u0442 \u0433\u043e\u0442\u043e\u0432! \u0411\u0443\u0434\u0435\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c!<\/p>\n<p>\u041d\u0430\u0445\u043e\u0434\u0438\u043c \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u043e\u0442\u0430 \u0432 telegram.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ac5\/4a6\/36c\/ac54a636c99c695b055354eae5380c4f.png\" width=\"686\" height=\"174\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ac5\/4a6\/36c\/ac54a636c99c695b055354eae5380c4f.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/15e\/0a3\/be8\/15e0a3be8f1d01ff02834b7ec677b869.png\" width=\"1736\" height=\"951\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/15e\/0a3\/be8\/15e0a3be8f1d01ff02834b7ec677b869.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 telegram \u0438 \u043d\u0430\u0436\u0438\u043c\u0430\u0435\u043c start \u0438\u043b\u0438 \u043d\u0430\u0431\u0438\u0440\u0430\u0435\u043c \/start \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/944\/e68\/84a\/944e6884a204e56d1aa22a4cbff5537b.png\" width=\"445\" height=\"149\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/944\/e68\/84a\/944e6884a204e56d1aa22a4cbff5537b.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041c\u043e\u0436\u0435\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043b\u044c\u0448\u0435. \u041d\u0430\u0448 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/199\/8e2\/6b8\/1998e26b8616c2e0dc94ebfe05e43529.png\" width=\"468\" height=\"669\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/199\/8e2\/6b8\/1998e26b8616c2e0dc94ebfe05e43529.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0412\u0441\u0435\u043c \u043a\u0442\u043e \u0447\u0438\u0442\u0430\u043b \u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e. \u041f\u043e\u043a\u0430.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/715384\/\"> https:\/\/habr.com\/ru\/post\/715384\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u0414\u0430-\u0434\u0430 \u0442\u0435\u043c\u0430 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u0430 \u0438 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0440\u0430\u0445 habr-a \u0435\u0441\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u0440\u043e \u0442\u043e, \u043a\u0430\u043a \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e telegram bota, \u043d\u043e \u044f \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u0440\u0438\u0441\u043a\u043d\u0443 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0443. \u041c\u043e\u0436\u0435\u0442 \u043a\u043e\u043c\u0443-\u0442\u043e \u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430 \u0438 \u043f\u043e\u043b\u0435\u0437\u043d\u0430.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0431\u043e\u0442\u0430 \u043d\u0443\u0436\u0435\u043d \u0430\u043a\u043a\u0430\u0443\u043d\u0442 \u0432 telegram.<\/p>\n<p>\u0418 \u0442\u0430\u043a \u043d\u0430\u0447\u043d\u0435\u043c: \u043d\u0430\u0448 \u0431\u043e\u0442 \u0431\u0443\u0434\u0435\u0442 &#171;\u0445\u043e\u0434\u0438\u0442\u044c&#187; \u043d\u0430 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0431\u0430\u043d\u043a\u0430 \u0438 \u0432\u044b\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u0443\u0440\u0441 \u043a \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u043e\u0439 \u0432\u0430\u043b\u044e\u0442\u0435.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0435\u043a\u0442, \u044f \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u044e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/start.spring.io\/\" rel=\"noopener noreferrer nofollow\">https:\/\/start.spring.io\/<\/a><\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0438 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0432 \u0441\u0440\u0435\u0434\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0439\u0442\u0438 \u0432 telegram \u0438 \u043d\u0430\u0439\u0442\u0438 \u0442\u0430\u043c <a href=\"https:\/\/habr.com\/users\/botfather.\" rel=\"noopener noreferrer nofollow\"><u>@BotFather.<\/u><\/a>  <\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0438\u0448\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \/newbot \u0438 \u0432\u0432\u043e\u0434\u0438\u043c \u0438\u043c\u044f \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u0431\u043e\u0442\u0430. \u0418\u043c\u044f \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 bot. \u041f\u043e \u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u0438 \u043d\u0430\u0441 \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u043a\u0430\u0442\u044c \u0432 telegram.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e BotFather \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u0430\u043c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043d\u0435\u043c\u0443. \u0414\u0430\u043d\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u043b\u0443\u0447\u0448\u0435 \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u0441\u0435\u043a\u0440\u0435\u0442\u0435 \u0438 \u043d\u0438\u043a\u043e\u043c\u0443 \u043d\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0443\u0437\u043d\u0430\u0432 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u0442\u043e\u043c\u0443 \u0431\u043e\u0442\u0443. \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f \u0435\u0433\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u044e.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0435\u043c \u0432 \u043d\u0430\u0448 \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043b\u044f telegram, lombok \u0438 json. \u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 json \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 jsona, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 API \u0431\u0430\u043d\u043a\u0430.<\/p>\n<pre><code class=\"java\">&lt;dependency> &lt;groupId>org.telegram&lt;\/groupId> &lt;artifactId>telegrambots&lt;\/artifactId> &lt;version>6.5.0&lt;\/version> &lt;\/dependency>  &lt;dependency> &lt;groupId>org.projectlombok&lt;\/groupId> &lt;artifactId>lombok&lt;\/artifactId> &lt;version>1.18.26&lt;\/version> &lt;scope>provided&lt;\/scope> &lt;\/dependency>      &lt;dependency> &lt;groupId>org.json&lt;\/groupId> &lt;artifactId>json&lt;\/artifactId> &lt;version>20220924&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<p>\u0412 application.properties \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0443 \u043a \u0431\u043e\u0442\u0443.<\/p>\n<pre><code class=\"java\">bot.name=OfficialRateOfBYNBot bot.token=5888893679:AAGQWAbS0zCMtnQrTeWrVYz9KHj2nUlyVIw<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 config, \u0438 \u0442\u0430\u043c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 BotConfig. \u041e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u043c\u0435\u043d\u0438 \u0438 \u0442\u043e\u043a\u0435\u043d\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u043e\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u043d\u0435\u043c\u0443.<\/p>\n<pre><code class=\"java\">@Configuration @Data @PropertySource(\"application.properties\") public class BotConfig {     @Value(\"${bot.name}\")     String botName;     @Value(\"${bot.token}\")     String token; }<\/code><\/pre>\n<p> \u0421\u0435\u0439\u0447\u0430\u0441 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u0440\u043e API \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u043a\u0443\u0440\u0441\u0430\u0445 \u0432\u0430\u043b\u044e\u0442. \u042f \u043d\u0430\u0448\u0435\u043b \u0442\u0430\u043a\u043e\u0439 <a href=\"https:\/\/www.nbrb.by\/apihelp\/exrates\" rel=\"noopener noreferrer nofollow\">https:\/\/www.nbrb.by\/apihelp\/exrates<\/a> \u043d\u0430 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u0430\u0439\u0442\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0431\u0430\u043d\u043a\u0430.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0442\u043f\u0440\u0430\u0432\u0438\u0432 get \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 API \u0447\u0435\u0440\u0435\u0437 postman \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 json.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u043b\u0435\u0433\u0447\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c json \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 CurrencyModel \u0432\u00a0\u043f\u0430\u043f\u043a\u0435 model<\/p>\n<pre><code class=\"java\">@Data public class CurrencyModel {     Integer cur_ID;     Date date;     String cur_Abbreviation;     Integer cur_Scale;     String cur_Name;     Double cur_OfficialRate; }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 CurrencyService \u0432\u00a0\u043f\u0430\u043f\u043a\u0435 service.<\/p>\n<pre><code class=\"java\">public class CurrencyService {      public static String getCurrencyRate(String message, CurrencyModel model) throws IOException, ParseException {         URL url = new URL(\"https:\/\/www.nbrb.by\/api\/exrates\/rates\/\" + message + \"?parammode=2\");         Scanner scanner = new Scanner((InputStream) url.getContent());         String result = \"\";         while (scanner.hasNext()){             result +=scanner.nextLine();         }         JSONObject object = new JSONObject(result);          model.setCur_ID(object.getInt(\"Cur_ID\"));         model.setDate(new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss\").parse(object.getString(\"Date\")));         model.setCur_Abbreviation(object.getString(\"Cur_Abbreviation\"));         model.setCur_Scale(object.getInt(\"Cur_Scale\"));         model.setCur_Name(object.getString(\"Cur_Name\"));         model.setCur_OfficialRate(object.getDouble(\"Cur_OfficialRate\"));           return \"Official rate of BYN to \" + model.getCur_Abbreviation() + \"\\n\" +                 \"on the date: \" + getFormatDate(model) + \"\\n\" +                 \"is: \" + model.getCur_OfficialRate() + \" BYN per \" + model.getCur_Scale() + \" \" + model.getCur_Abbreviation();      }      private static String getFormatDate(CurrencyModel model) {         return new SimpleDateFormat(\"dd MMM yyyy\").format(model.getDate());     } }<\/code><\/pre>\n<p>\u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u043c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 getCurrencyRate, \u0434\u0435\u043b\u0430\u0435\u043c \u0435\u0433\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a\u00a0\u043d\u0435\u043c\u0443 \u0431\u0435\u0437\u00a0\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. \u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u043c\u044b \u00ab\u0438\u0434\u0435\u043c\u00bb \u043d\u0430\u00a0\u0441\u0430\u0439\u0442 \u041d\u0411 \u0438 \u0442\u0430\u043c \u043f\u043e\u00a0\u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u043e\u0439 \u0432\u0430\u043b\u044e\u0442\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0435\u0451 \u043a\u0443\u0440\u0441 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432\u00a0\u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c CurrencyModel. \u041d\u0430\u00a0\u0432\u044b\u0445\u043e\u0434\u0435 \u0432\u044b\u0434\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435, \u0438\u0437\u00a0\u044d\u0442\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0443\u0436\u0435 \u0432\u00a0\u0442\u0430\u043a\u043e\u043c \u0432\u0438\u0434\u0435, \u0432\u00a0\u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0438\u0445 \u0431\u0443\u0434\u0443\u0442 \u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0430\u0448\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 TelegramBot \u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u0441\u044f \u043e\u0442\u00a0TelegramLongPollingBot.<\/p>\n<pre><code class=\"java\">@Component @AllArgsConstructor public class TelegramBot extends TelegramLongPollingBot {     private final BotConfig botConfig;      @Override     public String getBotUsername() {         return botConfig.getBotName();     }      @Override     public String getBotToken() {         return botConfig.getToken();     }      @Override     public void onUpdateReceived(Update update) {         CurrencyModel currencyModel = new CurrencyModel();         String currency = \"\";          if(update.hasMessage() &amp;&amp; update.getMessage().hasText()){             String messageText = update.getMessage().getText();             long chatId = update.getMessage().getChatId();              switch (messageText){                 case \"\/start\":                     startCommandReceived(chatId, update.getMessage().getChat().getFirstName());                     break;                 default:                     try {                         currency = CurrencyService.getCurrencyRate(messageText, currencyModel);                      } catch (IOException e) {                         sendMessage(chatId, \"We have not found such a currency.\" + \"\\n\" +                                 \"Enter the currency whose official exchange rate\" + \"\\n\" +                                 \"you want to know in relation to BYN.\" + \"\\n\" +                                 \"For example: USD\");                     } catch (ParseException e) {                         throw new RuntimeException(\"Unable to parse date\");                     }                     sendMessage(chatId, currency);             }         }      }      private void startCommandReceived(Long chatId, String name) {         String answer = \"Hi, \" + name + \", nice to meet you!\" + \"\\n\" +                 \"Enter the currency whose official exchange rate\" + \"\\n\" +                 \"you want to know in relation to BYN.\" + \"\\n\" +                 \"For example: USD\";         sendMessage(chatId, answer);     }      private void sendMessage(Long chatId, String textToSend){         SendMessage sendMessage = new SendMessage();         sendMessage.setChatId(String.valueOf(chatId));         sendMessage.setText(textToSend);         try {             execute(sendMessage);         } catch (TelegramApiException e) {          }     } }<\/code><\/pre>\n<p>\u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0432\u043d\u0435\u0434\u0440\u044f\u0435\u043c \u043a\u043b\u0430\u0441\u0441 BotConfig \u0441\u00a0\u043d\u0430\u0448\u0438\u043c\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u043c\u0438 \u0434\u043b\u044f\u00a0\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a\u00a0\u0431\u043e\u0442\u0443. \u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 TelegramLongPollingBot: <\/p>\n<p>getBotUsername()\u00a0\u2014 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0438\u043c\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u043e\u0442\u0430.<\/p>\n<p>getBotToken()\u00a0\u2014 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u043e\u0442\u0430.<\/p>\n<p>onUpdateReceived(Update update)\u00a0\u2014 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438\u00a0\u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c. \u041e\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u00a0\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c update, \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0435\u043a\u0441\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, id \u0447\u0430\u0442\u0430 \u0434\u043b\u044f\u00a0\u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u043e\u0442\u0432\u0435\u0442\u043d\u043e\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430:<\/p>\n<p>startCommandReceived(Long chatId, String name)\u00a0\u2014 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u043d\u0430\u0431\u043e\u0440\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \/start \u0432\u00a0telegram. <\/p>\n<p>sendMessage(Long chatId, String textToSend)\u00a0\u2014 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0447\u0435\u0440\u0435\u0437 id \u0447\u0430\u0442\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432\u00a0telegram.<\/p>\n<p>\u0418 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c BotInitializer \u0432\u00a0\u043f\u0430\u043f\u043a\u0435 config.<\/p>\n<pre><code class=\"java\">@Component public class BotInitializer {     private final TelegramBot telegramBot;     @Autowired     public BotInitializer(TelegramBot telegramBot) {         this.telegramBot = telegramBot;     }      @EventListener({ContextRefreshedEvent.class})     public void init()throws TelegramApiException{         TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);         try{             telegramBotsApi.registerBot(telegramBot);         } catch (TelegramApiException e){          }     } }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0435\u0441\u0441\u0438\u044e \u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0435\u0433\u043e telegram \u0431\u043e\u0442\u0430.<\/p>\n<p>\u041d\u0430\u0448 \u0431\u043e\u0442 \u0433\u043e\u0442\u043e\u0432! \u0411\u0443\u0434\u0435\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c!<\/p>\n<p>\u041d\u0430\u0445\u043e\u0434\u0438\u043c \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u043e\u0442\u0430 \u0432 telegram.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 telegram \u0438 \u043d\u0430\u0436\u0438\u043c\u0430\u0435\u043c start \u0438\u043b\u0438 \u043d\u0430\u0431\u0438\u0440\u0430\u0435\u043c \/start \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u041c\u043e\u0436\u0435\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043b\u044c\u0448\u0435. \u041d\u0430\u0448 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0412\u0441\u0435\u043c \u043a\u0442\u043e \u0447\u0438\u0442\u0430\u043b \u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e. \u041f\u043e\u043a\u0430.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/715384\/\"> https:\/\/habr.com\/ru\/post\/715384\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-345091","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=345091"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345091\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=345091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=345091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=345091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}