-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatBot.kt
107 lines (80 loc) · 3.68 KB
/
ChatBot.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import com.github.jnexil.skribe.*
import com.github.jnexil.skribe.test.*
import com.mrkostua.math.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
// Import the relevant Gemini classes
import com.adacore.gemini.*
import kotlinx.coroutines.flow.mapNotNull
class MyBot : Application() {
// Initialize Firebase
private val db = initializeFirebase()
// Define command handlers
init {
command("/start") { start(it) }
command("/help") { helpCommand(it) }
message { locationHandler(it) }
}
private fun initializeFirebase(): Firestore {
// Initialize Firebase logic here
// ...
return firestore // Assuming you have a Firestore instance
}
private suspend fun start(update: Update) {
val user = update.user
val message = """
Hi ${user.mentionHtml()}! 👋
<b><i>I'm Ada</i></b>, your friendly chat bot 🤖!
I'm here to help you <b><i>discover social events, community initiatives, and drives</i></b> happening around you.
Whether you're looking for <b><i>volunteer opportunities, charity events, or community gatherings</i></b>, I've got you covered!
Just share your <b><i>location</i></b> or <b><i>community name</i></b> with me, and I'll fetch the <b><i>latest happenings</i></b> in your area.
(<i>we are restricted with tasks, so kindly provide only your location in a word or so</i>)
<b><i>Let's explore together!</i></b> 🌍✨
""".trimIndent()
update.message.replyHtml(message, replyMarkup = ForceReply(selective = true))
}
private suspend fun helpCommand(update: Update) {
update.message.replyText("Help!")
}
private suspend fun locationHandler(update: Update) {
val userMessage = update.message.text.capitalize()
val collectionRef = db.collection("posts")
val cityAttribute = "communityName"
val cityOperation = "=="
// Query documents where the 'communityName' field contains the user's message
val documents = collectionRef.whereEqualTo(cityAttribute, userMessage).limit(5).get().await()
// Prepare a reply message with the fetched data
var replyText = ""
for (document in documents) {
val title = document.getString("title")?.capitalize() ?: ""
val community = document.getString("communityName")?.capitalize() ?: ""
var description = document.getString("description")?.capitalize() ?: ""
if (description.isNotBlank()) {
description = "ℹ️ Description: $description\n"
}
val username = document.getString("username")?.capitalize() ?: ""
replyText += """
<b>📌 Title:</b> $title
<b>🌍 Community:</b> $community
$description
<b>👤 Username:</b> $username
""".trimIndent()
}
if (replyText.isBlank()) {
replyText =
"<b>No data found for the given location.</b>\n\n<i>Probably you've entered something mismatched, kindly try with something else.</i>"
} else {
replyText = "<b>Here's a list of some exciting social drives happening in $userMessage! 🌟</b>\n\n$replyText"
}
// Split the replyText into chunks of maximum message length allowed by Telegram
val messageChunks = replyText.chunked(4096)
// Reply to the user with each chunk separately
messageChunks.forEach {
update.message.replyHtml(it)
}
}
}
fun main() {
MyBot().run()
}