· 8 years ago · Jun 17, 2018, 06:48 AM
1package com.company;
2
3import net.aksingh.owmjapis.api.APIException;
4import net.aksingh.owmjapis.core.OWM;
5import net.aksingh.owmjapis.model.CurrentWeather;
6import org.telegram.telegrambots.ApiContextInitializer;
7import org.telegram.telegrambots.TelegramBotsApi;
8import org.telegram.telegrambots.api.methods.send.SendMessage;
9import org.telegram.telegrambots.api.objects.Location;
10import org.telegram.telegrambots.api.objects.Update;
11import org.telegram.telegrambots.bots.TelegramLongPollingBot;
12import org.telegram.telegrambots.exceptions.TelegramApiException;
13import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
14
15import java.sql.*;
16
17class Weather{
18
19 public static void location(MyWeatherBot bot){
20
21 }
22
23 public static String getCurrentWeather() {
24 String currentWeather = null;
25 String city = "Saint Petersburg";
26 OWM owm = new OWM("1862c8835eec744e12928401cb0b8f33");
27 CurrentWeather cw;
28
29 {
30 try {
31 cw = owm.currentWeatherByCityName(city);
32 System.out.print("temperature - ");
33 System.out.println(cw.getMainData().getTemp()-273.15);
34 //System.out.println(cw.getRainData().getPrecipVol3h().toString());
35 System.out.println("Cloud - "+cw.getCloudData().getCloud()+"%");
36 System.out.println("Humidity - "+cw.getMainData().getHumidity());
37 System.out.println("wind speed - "+cw.getWindData().getSpeed());
38
39 return ("temperature - "+Double.toString(cw.getMainData().getTemp()-273.15)+"; Cloud - "+cw.getCloudData().getCloud()+"%; "+"Humidity - "+cw.getMainData().getHumidity()+"; wind speed - "+cw.getWindData().getSpeed());
40 } catch (APIException e) {
41 e.printStackTrace();
42 return "unsuccessfull";
43 }
44 }
45
46 }
47 public static void subscribe(long chatId){
48 String url = "jdbc:sqlite:SusbscribeDb";
49 String addSubscribe = "INSERT INTO 'users' ('chatId') VALUES ("+chatId+");";
50 Connection connection;
51 Statement stmt;
52 ResultSet rs;
53 try {
54 connection = DriverManager.getConnection(url);
55 stmt = connection.createStatement();
56 stmt.execute("CREATE TABLE if not exists 'users' ('id' INTEGER PRIMARY KEY AUTOINCREMENT, 'chatId' INT);");
57 stmt.execute(addSubscribe);
58 rs = stmt.executeQuery("SELECT * FROM 'USERS'");
59 while (rs.next()){
60 System.out.println(rs.getString("id"));
61 System.out.println(rs.getString("chatId"));
62 }
63 } catch (SQLException e) {
64 e.printStackTrace();
65 }
66 }
67 public static void dispatch (MyWeatherBot bot){
68 String url = "jdbc:sqlite:SusbscribeDb";
69 String message_text = null;
70 Connection connection;
71 Statement stmt;
72 ResultSet rs;
73 String lon = "59.89";
74 String lat = "30.26";
75 try {
76 connection = DriverManager.getConnection(url);
77 stmt = connection.createStatement();
78 stmt.execute("CREATE TABLE if not exists 'users' ('id' INTEGER PRIMARY KEY AUTOINCREMENT, 'chatId' INT);");
79 rs = stmt.executeQuery("SELECT * FROM 'USERS'");
80 while (rs.next()){
81 System.out.println(rs.getString("id"));
82 System.out.println(rs.getString("chatId"));
83 message_text = Weather.getCurrentWeather();
84 SendMessage sendMsg = new SendMessage();
85 sendMsg.setChatId(rs.getString("chatId"));
86 sendMsg.setText(message_text);
87 bot.execute(sendMsg);
88 }
89 } catch (SQLException e) {
90 e.printStackTrace();
91 } catch (TelegramApiException e) {
92 e.printStackTrace();
93 }
94 }
95 }
96
97class MyWeatherBot extends TelegramLongPollingBot {
98 enum commands{current, subscribe}
99 String message_text;
100 @Override
101 public void onUpdateReceived(Update update) {
102
103 if (update.hasMessage() && update.getMessage().hasText()) {
104 System.out.println(update.getMessage().getText());
105 switch (update.getMessage().getText().toLowerCase()) {
106 case "/help":
107 message_text = "/current - Ñ‚ÐµÐºÑƒÑ‰Ð°Ñ Ð¿Ð¾Ð³Ð¾Ð´Ð°, /subscribe - подпиÑка на получение погоды";
108 return;
109 case "/current":
110 message_text =Weather.getCurrentWeather();
111 break;
112
113 case "/subscribe":
114 Weather.subscribe(update.getMessage().getChatId());
115 message_text="you are subscribed";
116 break;
117
118 default: message_text = "i don't understand you";
119 }
120 long chat_id = update.getMessage().getChatId();
121 SendMessage message = new SendMessage() // Create a message object object
122 .setChatId(chat_id)
123 .setText(message_text);
124 try {
125 execute(message); // Sending our message object to user
126 } catch (TelegramApiException e) {
127 e.printStackTrace();
128 }
129 }
130 }
131
132 @Override
133 public String getBotUsername() {
134 return "myWetherBot";
135 }
136
137 @Override
138 public String getBotToken() {
139 return "560059219:AAFcwJOmrZbSOP2hlLvlsGtdvuWVS8FQ7oY";
140 }
141}
142
143public class Main {
144
145 public static void myThread(MyWeatherBot bot){
146 Thread thread = new Thread() {
147 @Override
148 public void run() {
149 while (true) {
150 Weather.dispatch(bot);
151 try {
152 Thread.sleep(60000);
153 } catch (InterruptedException e) {
154 e.printStackTrace();
155 }
156 }
157 }
158 };
159 thread.start();
160 }
161
162 public static void main(String[] args) {
163 // write your code here
164 ApiContextInitializer.init();
165 TelegramBotsApi botsApi = new TelegramBotsApi();
166
167 try {
168 MyWeatherBot bot = new MyWeatherBot();
169 botsApi.registerBot(bot);
170 myThread(bot);
171 } catch (TelegramApiRequestException e) {
172 e.printStackTrace();
173 }
174 System.out.println("bot is run");
175 }
176}