mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-09-05 16:19:43 +00:00
fixes
This commit is contained in:
@@ -11,7 +11,7 @@ buildscript {
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="com.insanusmokrassar.examples.ForwarderBotKt"
|
||||
mainClassName="ForwarderBotKt"
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
|
42
ForwarderBot/src/main/kotlin/ForwarderBot.kt
Normal file
42
ForwarderBot/src/main/kotlin/ForwarderBot.kt
Normal file
@@ -0,0 +1,42 @@
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.send.sendTextMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.telegramBot
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.formatting.codeMarkdownV2
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.formatting.regularMarkdownV2
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.safely
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.asContentMessagesFlow
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownV2
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.PossiblyForwardedMessage
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
/**
|
||||
* This bot will always return message about forwarder. In cases when sent message was not a forward message it will
|
||||
* send suitable message
|
||||
*/
|
||||
suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
|
||||
val bot = telegramBot(botToken)
|
||||
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
|
||||
bot.startGettingFlowsUpdatesByLongPolling(scope = scope) {
|
||||
messageFlow.asContentMessagesFlow().mapNotNull { it as? PossiblyForwardedMessage }.onEach { message ->
|
||||
safely {
|
||||
val toAnswer = when (val forwardInfo = message.forwardInfo) {
|
||||
null -> "There is no forward info"
|
||||
is AnonymousForwardInfo -> "Anonymous user which signed as \"${forwardInfo.senderName.codeMarkdownV2()}\""
|
||||
is UserForwardInfo -> forwardInfo.from.let { user ->
|
||||
"User ${user.id.chatId.toString().codeMarkdownV2()} " + "(${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})".regularMarkdownV2()
|
||||
}
|
||||
is ForwardFromChannelInfo -> "Channel (".regularMarkdownV2() + (forwardInfo.channelChat).title.codeMarkdownV2() + ")".regularMarkdownV2()
|
||||
}
|
||||
bot.sendTextMessage(message.chat, toAnswer, MarkdownV2)
|
||||
}
|
||||
}.launchIn(scope)
|
||||
}
|
||||
|
||||
scope.coroutineContext[Job]!!.join()
|
||||
}
|
@@ -1,51 +0,0 @@
|
||||
package com.insanusmokrassar.examples
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorRequestsExecutor
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.send.sendTextMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownV2
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.ChannelChat
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.PossiblyForwardedMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.startGettingOfUpdates
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
/**
|
||||
* This bot will always return message about forwarder. In cases when sent message was not a forward message it will
|
||||
* send suitable message
|
||||
*/
|
||||
suspend fun main(args: Array<String>) {
|
||||
val botToken = args.first()
|
||||
|
||||
val bot = KtorRequestsExecutor(TelegramAPIUrlsKeeper(botToken))
|
||||
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
|
||||
val callback: UpdateReceiver<BaseMessageUpdate> = { messageUpdate ->
|
||||
val message = messageUpdate.data
|
||||
val infoToSend = if (message is PossiblyForwardedMessage) {
|
||||
val forwardInfo = message.forwardInfo
|
||||
when (forwardInfo) {
|
||||
null -> "There is no forward info"
|
||||
is AnonymousForwardInfo -> "Anonymous user which signed as \"${forwardInfo.senderName.codeMarkdownV2()}\""
|
||||
is UserForwardInfo -> forwardInfo.from.let { user ->
|
||||
"User ${user.id.chatId.toString().codeMarkdownV2()} " + "(${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})".regularMarkdownV2()
|
||||
}
|
||||
is ForwardFromChannelInfo -> "Channel (".regularMarkdownV2() + (forwardInfo.channelChat).title.codeMarkdownV2() + ")".regularMarkdownV2()
|
||||
}
|
||||
} else {
|
||||
"There is no forward info"
|
||||
}
|
||||
bot.sendTextMessage(message.chat, infoToSend, MarkdownV2)
|
||||
}
|
||||
|
||||
bot.startGettingOfUpdates(
|
||||
messageCallback = callback,
|
||||
channelPostCallback = callback,
|
||||
scope = scope
|
||||
)
|
||||
|
||||
scope.coroutineContext[Job]!!.join()
|
||||
}
|
Reference in New Issue
Block a user