Skip to content

Commit

Permalink
analysis module
Browse files Browse the repository at this point in the history
  • Loading branch information
demidko committed Sep 26, 2024
1 parent c41dabe commit aa1e7c9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/main/kotlin/com/github/demidko/glock/ApplicationFactory.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.demidko.glock

import com.github.kotlintelegrambot.entities.ChatId.Companion.fromId
import com.github.kotlintelegrambot.entities.ChatPermissions
import com.sksamuel.hoplite.ConfigLoaderBuilder
import com.sksamuel.hoplite.ExperimentalHoplite
Expand All @@ -13,9 +12,10 @@ import kotlin.time.toKotlinDuration
open class ApplicationFactory {
data class Config(
val botToken: String,
val excludedChatsUsernames: Set<String>,
val healingConstant: Long = 7,
val healingTimeZone: String = "Asia/Jerusalem",
val restrictionsDuration: Duration = ofMinutes(5)
val restrictionsDuration: Duration = ofMinutes(5),
)

@OptIn(ExperimentalHoplite::class)
Expand Down Expand Up @@ -43,13 +43,18 @@ open class ApplicationFactory {
canPinMessages = false
)

open val spyModule by lazy {
SpyModule(config.excludedChatsUsernames)
}

open val glockBot by lazy {
GlockBot(
config.botToken,
restrictions,
config.restrictionsDuration,
config.healingConstant,
ZoneId.of(config.healingTimeZone)
ZoneId.of(config.healingTimeZone),
spyModule
)
}
}
11 changes: 8 additions & 3 deletions src/main/kotlin/com/github/demidko/glock/GlockBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.github.kotlintelegrambot.dispatcher.message
import com.github.kotlintelegrambot.entities.ChatId.Companion.fromId
import com.github.kotlintelegrambot.entities.ChatPermissions
import com.github.kotlintelegrambot.entities.Message
import java.io.Closeable
import java.lang.Thread.startVirtualThread
import java.time.Duration
import java.time.Duration.ofDays
Expand All @@ -22,7 +21,8 @@ class GlockBot(
private val restrictions: ChatPermissions,
private val restrictionsDuration: Duration,
private val healingConstant: Long,
private val healingTimeZone: ZoneId
private val healingTimeZone: ZoneId,
private val spyModule: SpyModule
) {

init {
Expand All @@ -43,6 +43,11 @@ class GlockBot(
command("help", handleCommand(ChatOps::help))
command("start", handleCommand(ChatOps::help))
message(handleMessage(ChatOps::tryProcessStatuette))
message {
startVirtualThread {
spyModule.spy(message)
}
}
}
}

Expand All @@ -51,7 +56,7 @@ class GlockBot(
private var previousChatsCount = 0

fun cleanTempMessages() {
if(idToChatOps.count() > previousChatsCount) {
if (idToChatOps.count() > previousChatsCount) {
previousChatsCount = idToChatOps.count()
println("Bot has $previousChatsCount chats")
}
Expand Down
59 changes: 59 additions & 0 deletions src/main/kotlin/com/github/demidko/glock/SpyModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.demidko.glock

import com.github.kotlintelegrambot.entities.Chat
import com.github.kotlintelegrambot.entities.Message

class SpyModule(private val excludedChatsUsernames: Set<String>) {

fun spy(m: Message) {
if (isExcludedChat(m)) {
return
}
println(m)
/*val user = readUser(m)
val chat = readChat(m)
val gist = readGist(m)
println("$user — $chat: $gist")*/
}

private fun isExcludedChat(m: Message): Boolean {
return m.chat.username in excludedChatsUsernames
}

private fun readChat(m: Message): String {
return readChat(m.chat)
}

private fun readChat(chat: Chat): String {
if (chat.username != null) {
return chat.username!!
}
if (chat.inviteLink != null) {
return chat.inviteLink!!
}
if (chat.location != null) {
val location = chat.location!!
return "${location.location} ${location.address}"
}
return buildString {
append("Group id ")
append(chat.id)
if (chat.firstName != null) {
append(' ')
append(chat.firstName)
}
if (chat.lastName != null) {
append(' ')
append(chat.lastName)
}
}
}

private fun readUser(m: Message): String {
if (m.from == null) {

}
m.senderChat
TODO()
}
}

0 comments on commit aa1e7c9

Please sign in to comment.