1
1
package org.moztw.bot.telegram.space
2
2
3
3
import io.prometheus.client.Counter
4
+ import io.prometheus.client.Gauge
4
5
import okhttp3.OkHttpClient
5
6
import okhttp3.Request
6
7
import org.moztw.bot.telegram.space.co2.SpaceCo2
@@ -24,10 +25,11 @@ internal class Bot(val username: String, val token: String) : TelegramLongPollin
24
25
updates.inc()
25
26
println (" * [$dateTime ] Update received: $update " )
26
27
27
- if (! (update.hasMessage() && onMessageReceived(update.message) || update.hasCallbackQuery() && onCallbackQueryReceived(update.callbackQuery))) {
28
- updatesUnhandled.inc()
29
- println (" * [$dateTime ] Update not handled: $update " )
30
- }
28
+ if (update.hasMessage() && onMessageReceived(update.message)) return
29
+ if (update.hasCallbackQuery() && onCallbackQueryReceived((update.callbackQuery))) return
30
+
31
+ updatesUnhandled.inc()
32
+ println (" * [$dateTime ] Update not handled: $update " )
31
33
}
32
34
33
35
private fun onCallbackQueryReceived (callbackQuery : CallbackQuery ): Boolean {
@@ -104,9 +106,12 @@ internal class Bot(val username: String, val token: String) : TelegramLongPollin
104
106
}
105
107
}
106
108
}
109
+ return true
107
110
} else if (isAdminChat(message.chat)) {
108
111
if (isCommandOpen(message.text)) {
109
112
commandCalls.labels(" space_open" ).inc()
113
+ spaceDoorState.set(1.0 )
114
+ spaceDoorStateTime.setToCurrentTime()
110
115
if (tryExecute(Caption ().getCaptionOpened(chatId = generalChatId))
111
116
&& tryExecute(Reply ().getGeneralMessageOpen(chatId = generalChatId, operator = message.from))
112
117
&& tryExecute(Reply ().getMessageOpen(message = message))) {
@@ -118,6 +123,8 @@ internal class Bot(val username: String, val token: String) : TelegramLongPollin
118
123
}
119
124
} else if (isCommandClose(message.text)) {
120
125
commandCalls.labels(" space_close" ).inc()
126
+ spaceDoorState.set(0.0 )
127
+ spaceDoorStateTime.setToCurrentTime()
121
128
if (tryExecute(Caption ().getCaptionClosed(chatId = generalChatId))
122
129
&& tryExecute(Reply ().getGeneralMessageClose(chatId = generalChatId, operator = message.from))
123
130
&& tryExecute(Reply ().getMessageClose(message = message))) {
@@ -218,5 +225,15 @@ internal class Bot(val username: String, val token: String) : TelegramLongPollin
218
225
.help(" Total Telegram Bots API calls." )
219
226
.labelNames(" method" )
220
227
.register()
228
+
229
+ val spaceDoorState: Gauge = Gauge .build()
230
+ .name(" moztw_space_door_state" )
231
+ .help(" The state of MozTW Space Door. 0 = closed, 1 = opened" )
232
+ .register()
233
+
234
+ val spaceDoorStateTime: Gauge = Gauge .build()
235
+ .name(" moztw_space_door_state_time" )
236
+ .help(" The update time for the state of MozTW Space Door." )
237
+ .register()
221
238
}
222
239
}
0 commit comments