TelegramBotAPI-examples/ForwarderBot/src/main/kotlin/com/insanusmokrassar/examples/ForwarderBot.kt

51 lines
2.2 KiB
Kotlin

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.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.TelegramAPIUrlsKeeper
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}\""
is UserForwardInfo -> forwardInfo.from.let { user ->
"User `${user.id.chatId}` (`${user.firstName} ${user.lastName}`: ${user.username ?.username ?: "Without username"})"
}
is ForwardFromChannelInfo -> "Channel (${forwardInfo.channelChat})"
}
} else {
"There is no forward info"
}
bot.sendTextMessage(message.chat, infoToSend, MarkdownV2)
}
bot.startGettingOfUpdates(
messageCallback = callback,
channelPostCallback = callback,
scope = scope
)
scope.coroutineContext[Job]!!.join()
}