mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-09-04 07:09:04 +00:00
Compare commits
15 Commits
3.3.0
...
4186ab8270
| Author | SHA1 | Date | |
|---|---|---|---|
| 4186ab8270 | |||
| 5aa69d7990 | |||
|
|
df952c69b2 | ||
| 9a03a02bac | |||
| 0e7c050e9e | |||
|
|
bdec902b58 | ||
|
|
cc3c87590d | ||
| 910f892b89 | |||
| 8232cb4d62 | |||
| 3b26971152 | |||
| c0019bcbf8 | |||
| c3dcb4d738 | |||
| 6a9921d4bd | |||
| 33b14f320c | |||
| 50ad281132 |
9
ChatAvatarSetter/README.md
Normal file
9
ChatAvatarSetter/README.md
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# ChatAvatarSetter
|
||||||
|
|
||||||
|
This bot will set the chat avatar based on the image sent to bot
|
||||||
|
|
||||||
|
## Launch
|
||||||
|
|
||||||
|
```bash
|
||||||
|
../gradlew run --args="BOT_TOKEN"
|
||||||
|
```
|
||||||
21
ChatAvatarSetter/build.gradle
Normal file
21
ChatAvatarSetter/build.gradle
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apply plugin: 'kotlin'
|
||||||
|
apply plugin: 'application'
|
||||||
|
|
||||||
|
mainClassName="ChatAvatarSetterKt"
|
||||||
|
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|
||||||
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
|
}
|
||||||
36
ChatAvatarSetter/src/main/kotlin/ChatAvatarSetter.kt
Normal file
36
ChatAvatarSetter/src/main/kotlin/ChatAvatarSetter.kt
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||||
|
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.modify.setChatPhoto
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPhoto
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.*
|
||||||
|
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
|
suspend fun main(args: Array<String>) {
|
||||||
|
val bot = telegramBot(args.first())
|
||||||
|
|
||||||
|
bot.buildBehaviourWithLongPolling(scope = CoroutineScope(Dispatchers.IO)) {
|
||||||
|
onPhoto {
|
||||||
|
val bytes = downloadFile(it.content)
|
||||||
|
runCatchingSafely {
|
||||||
|
setChatPhoto(
|
||||||
|
it.chat.id,
|
||||||
|
bytes.asMultipartFile("sample.jpg")
|
||||||
|
)
|
||||||
|
}.onSuccess { b ->
|
||||||
|
if (b) {
|
||||||
|
reply(it, "Done")
|
||||||
|
} else {
|
||||||
|
reply(it, "Something went wrong")
|
||||||
|
}
|
||||||
|
}.onFailure { e ->
|
||||||
|
e.printStackTrace()
|
||||||
|
|
||||||
|
reply(it, "Something went wrong (see logs)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.join()
|
||||||
|
}
|
||||||
@@ -6,17 +6,21 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
|||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
|
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.extensions.sameThread
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||||
import dev.inmo.tgbotapi.types.ChatId
|
import dev.inmo.tgbotapi.types.IdChatIdentifier
|
||||||
|
import dev.inmo.tgbotapi.types.MessageThreadId
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||||
import dev.inmo.tgbotapi.utils.botCommand
|
import dev.inmo.tgbotapi.utils.botCommand
|
||||||
|
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
import kotlinx.coroutines.flow.filter
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
|
|
||||||
sealed interface BotState : State
|
sealed interface BotState : State
|
||||||
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : BotState
|
data class ExpectContentOrStopState(override val context: IdChatIdentifier, val sourceMessage: CommonMessage<TextContent>) : BotState
|
||||||
data class StopState(override val context: ChatId) : BotState
|
data class StopState(override val context: IdChatIdentifier) : BotState
|
||||||
|
|
||||||
suspend fun main(args: Array<String>) {
|
suspend fun main(args: Array<String>) {
|
||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
@@ -39,12 +43,14 @@ suspend fun main(args: Array<String>) {
|
|||||||
) {
|
) {
|
||||||
strictlyOn<ExpectContentOrStopState> {
|
strictlyOn<ExpectContentOrStopState> {
|
||||||
send(
|
send(
|
||||||
it.context
|
it.context,
|
||||||
) {
|
) {
|
||||||
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
||||||
}
|
}
|
||||||
|
|
||||||
val contentMessage = waitContentMessage().first()
|
val contentMessage = waitContentMessage().filter { message ->
|
||||||
|
message.sameThread(it.sourceMessage)
|
||||||
|
}.first()
|
||||||
val content = contentMessage.content
|
val content = contentMessage.content
|
||||||
|
|
||||||
when {
|
when {
|
||||||
@@ -61,7 +67,9 @@ suspend fun main(args: Array<String>) {
|
|||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
command("start") {
|
command(
|
||||||
|
"start"
|
||||||
|
) {
|
||||||
startChain(ExpectContentOrStopState(it.chat.id, it))
|
startChain(ExpectContentOrStopState(it.chat.id, it))
|
||||||
}
|
}
|
||||||
}.second.join()
|
}.second.join()
|
||||||
|
|||||||
@@ -1,5 +1,16 @@
|
|||||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.forum.closeForumTopic
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.forum.createForumTopic
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.forum.deleteForumTopic
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.forum.reopenForumTopic
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onForumTopicClosed
|
||||||
|
import dev.inmo.tgbotapi.types.ChatId
|
||||||
|
import dev.inmo.tgbotapi.types.CustomEmojiId
|
||||||
|
import dev.inmo.tgbotapi.types.ForumTopic
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is one of the most easiest bot - it will just print information about itself
|
* This is one of the most easiest bot - it will just print information about itself
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilte
|
|||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
||||||
|
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
suspend fun activateResenderBot(
|
suspend fun activateResenderBot(
|
||||||
@@ -20,33 +21,23 @@ suspend fun activateResenderBot(
|
|||||||
|
|
||||||
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
|
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
|
||||||
onContentMessage(
|
onContentMessage(
|
||||||
initialFilter = CommonMessageFilterExcludeMediaGroups,
|
subcontextUpdatesFilter = MessageFilterByChat,
|
||||||
subcontextUpdatesFilter = MessageFilterByChat
|
|
||||||
) {
|
) {
|
||||||
val chat = it.chat
|
val chat = it.chat
|
||||||
withTypingAction(chat) {
|
|
||||||
executeUnsafe(it.content.createResend(chat.id, replyToMessageId = it.messageId)) {
|
val answer = withTypingAction(chat) {
|
||||||
|
executeUnsafe(
|
||||||
|
it.content.createResend(
|
||||||
|
chat.id,
|
||||||
|
messageThreadId = it.threadIdOrNull,
|
||||||
|
replyToMessageId = it.messageId
|
||||||
|
)
|
||||||
|
) {
|
||||||
it.forEach(print)
|
it.forEach(print)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
onVisualGallery {
|
println("Answer info: $answer")
|
||||||
val chat = it.chat ?: return@onVisualGallery
|
|
||||||
withUploadPhotoAction(chat) {
|
|
||||||
send(chat, it.map { it.content.toMediaGroupMemberTelegramMedia() })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
onPlaylist {
|
|
||||||
val chat = it.chat ?: return@onPlaylist
|
|
||||||
withUploadDocumentAction(chat) {
|
|
||||||
send(chat, it.map { it.content.toMediaGroupMemberTelegramMedia() })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
onDocumentsGroup {
|
|
||||||
val chat = it.chat ?: return@onDocumentsGroup
|
|
||||||
withUploadDocumentAction(chat) {
|
|
||||||
send(chat, it.map { it.content.toMediaGroupMemberTelegramMedia() })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ org.gradle.parallel=true
|
|||||||
org.gradle.jvmargs=-Xmx768m
|
org.gradle.jvmargs=-Xmx768m
|
||||||
|
|
||||||
|
|
||||||
kotlin_version=1.7.20
|
kotlin_version=1.7.21
|
||||||
telegram_bot_api_version=3.3.0
|
telegram_bot_api_version=4.1.0
|
||||||
micro_utils_version=0.13.1
|
micro_utils_version=0.14.1
|
||||||
serialization_version=1.4.1
|
serialization_version=1.4.1
|
||||||
ktor_version=2.1.2
|
ktor_version=2.1.3
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ include ":StickerInfoBot:jvm_launcher"
|
|||||||
|
|
||||||
include ":SlotMachineDetectorBot"
|
include ":SlotMachineDetectorBot"
|
||||||
|
|
||||||
|
include ":ChatAvatarSetter"
|
||||||
|
|
||||||
include ":WebApp"
|
include ":WebApp"
|
||||||
|
|
||||||
include ":FSMBot"
|
include ":FSMBot"
|
||||||
|
|||||||
Reference in New Issue
Block a user