Ver código fonte

получаем уведомления, ура

kpmy 8 anos atrás
pai
commit
8584bd17a5

+ 4 - 0
.gitignore

@@ -12,3 +12,7 @@
 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
 hs_err_pid*
 
+/target
+*.iml
+*.ipr
+*.iws

+ 59 - 0
pom.xml

@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xmlns="http://maven.apache.org/POM/4.0.0"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>in.ocsf</groupId>
+    <artifactId>these-days</artifactId>
+    <version>0.0.1</version>
+    <packaging>war</packaging>
+    <properties>
+        <java.version>1.8</java.version>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>1.5.1.RELEASE</version>
+    </parent>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-mongodb</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-rest</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-tomcat</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>com.github.pengrad</groupId>
+            <artifactId>java-telegram-bot-api</artifactId>
+            <version>2.3.1.1</version>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-war-plugin</artifactId>
+                <version>3.0.0</version>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 25 - 0
src/main/java/in/ocsf/these/days/app/App.java

@@ -0,0 +1,25 @@
+package in.ocsf.these.days.app;/* kpmy 19.02.2017 */
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.boot.web.support.SpringBootServletInitializer;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.annotation.EnableScheduling;
+
+@SpringBootApplication
+@EnableScheduling
+@EnableAsync
+public class App extends SpringBootServletInitializer {
+
+    private static Class<App> applicationClass = App.class;
+
+    public static void main(String[] args) {
+        SpringApplication.run(applicationClass, args);
+    }
+
+    @Override
+    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
+        return application.sources(applicationClass);
+    }
+}

+ 67 - 0
src/main/java/in/ocsf/these/days/app/listener/ChatListener.java

@@ -0,0 +1,67 @@
+package in.ocsf.these.days.app.listener;/* kpmy 19.02.2017 */
+
+import com.pengrad.telegrambot.Callback;
+import com.pengrad.telegrambot.TelegramBot;
+import com.pengrad.telegrambot.TelegramBotAdapter;
+import com.pengrad.telegrambot.model.Update;
+import com.pengrad.telegrambot.request.GetUpdates;
+import com.pengrad.telegrambot.response.GetUpdatesResponse;
+import in.ocsf.these.days.app.object.UpdateState;
+import in.ocsf.these.days.app.repo.UpdateStateRepository;
+import org.apache.log4j.Logger;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+import java.util.Optional;
+
+@Service
+public class ChatListener {
+
+    private final Logger log = Logger.getLogger(getClass());
+
+    @Value("${these-days.bot.token}")
+    private String token;
+
+    @Autowired
+    private UpdateStateRepository stateRepo;
+
+    @Async
+    protected void update(Update update) {
+        log.info(update.updateId());
+    }
+
+    @Scheduled(fixedDelay = 500)
+    protected void doRequest() {
+        TelegramBot bot = TelegramBotAdapter.build(token);
+
+        final UpdateState state = Optional.ofNullable(stateRepo.findOne(UpdateState.defaultId)).orElse(new UpdateState());
+        GetUpdates request = new GetUpdates().limit(1024).offset(state.getUpdateId().intValue() + 1);
+
+        bot.execute(request, new Callback<GetUpdates, GetUpdatesResponse>() {
+
+            @Override
+            public void onResponse(GetUpdates request, GetUpdatesResponse response) {
+                if (response.isOk()) {
+                    if (response.updates() != null)
+                        response.updates().stream().forEach(u -> {
+                            update(u);
+                            if (u.updateId() > state.getUpdateId())
+                                state.setUpdateId(Long.valueOf(u.updateId()));
+                        });
+                    stateRepo.save(state);
+                } else {
+                    throw new RuntimeException("response not ok");
+                }
+            }
+
+            @Override
+            public void onFailure(GetUpdates request, IOException e) {
+                e.printStackTrace();
+            }
+        });
+    }
+}

+ 23 - 0
src/main/java/in/ocsf/these/days/app/object/UpdateState.java

@@ -0,0 +1,23 @@
+package in.ocsf.these.days.app.object;/* kpmy 19.02.2017 */
+
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+@Document(collection = "update-state")
+public class UpdateState {
+
+    public static final String defaultId = "last-update";
+
+    @Id
+    private String id = defaultId;
+
+    private Long updateId = 0L;
+
+    public Long getUpdateId() {
+        return updateId;
+    }
+
+    public void setUpdateId(Long updateId) {
+        this.updateId = updateId;
+    }
+}

+ 7 - 0
src/main/java/in/ocsf/these/days/app/repo/UpdateStateRepository.java

@@ -0,0 +1,7 @@
+package in.ocsf.these.days.app.repo;/* kpmy 19.02.2017 */
+
+import in.ocsf.these.days.app.object.UpdateState;
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+public interface UpdateStateRepository extends MongoRepository<UpdateState, String> {
+}

+ 4 - 0
src/main/resources/application-dev.properties

@@ -0,0 +1,4 @@
+spring.data.mongodb.uri=mongodb://localhost:27017/these-days
+spring.data.rest.base-path=/api
+these-days.bot.token=224198257:AAFJeYcnaqNSqql5RE0zlcqHy_JeKVdDL2M
+these-days.debug=true

+ 6 - 0
src/main/resources/banner.txt

@@ -0,0 +1,6 @@
+  _   _                          _
+ | |_| |__   ___  ___  ___    __| | __ _ _   _ ___
+ | __| '_ \ / _ \/ __|/ _ \  / _` |/ _` | | | / __|
+ | |_| | | |  __/\__ \  __/ | (_| | (_| | |_| \__ \
+  \__|_| |_|\___||___/\___|  \__,_|\__,_|\__, |___/
+                                         |___/

+ 14 - 0
src/main/webapp/WEB-INF/web.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
+         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
+                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
+         version="4.0">
+
+    <servlet>
+        <servlet-name>these-days</servlet-name>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+
+
+</web-app>