Kotlin
(Shengyou Fan)
Chatbot Meetup #28
2021/03/15
Photo by Phillip Glickman on Unsplash
—
•
•
•
SDK
•
Kotlin Open Source
JetBrains …
Chatbot
2020 3 Kotlin
!
…
Kotlin …
+
—
60
2020
100
2021
Kotlin
—
• General-purpose
• Static typing
• OOP + FP
• Developed by
JetBrains
• Open Source
(Apache 2.0)
https://kotlinlang.org/
—
Kotlin
—
Browser
Kotlin/JS
Server
Kotlin/JVM
iOS
Kotlin/Native
Android
Kotlin/JVM
—
•
•
•
•
Email
Email
—
•
•
•
IM
Kotlin
~
—
• IntelliJ IDEA JDKs
• Telegram Bot
• LINE Developer Messaging API
• ngrok
IntelliJ IDEA
—
IDE + SDK
Telegram Bot
—
@BotFather
LINE Developer
—
Messaging API
—
ngrok
—
# macOS
$ brew install --cask ngrok
# Windows
$ scoop install ngrok
"
Kotlin Telegram SDK
—
https://github.com/kotlin-telegram-bot/kotlin-telegram-bot
Kotlin
—
—
repositories {
// ...
maven("https://jitpack.io")
}
dependencies {
// ...
implementation("...kotlin-telegram-bot:telegram:$version")
// ...
}
Bot
—
val bot = bot {
token = ...
// ...
}
bot.startPolling()
EchoBot
—
bot {
token = ...
dispatch {
text {
bot.sendMessage(
chatId = ChatId.fromId(message.chat.id),
text = text
)
}
}
}
—
bot {
// ...
dispatch {
text("ping") {
bot.sendMessage(
chatId = ChatId.fromId(message.chat.id),
text = "..."
)
}
text("markdown") {
val markdownText = "..."
bot.sendMessage(
chatId = ChatId.fromId(message.chat.id),
text = markdownText,
parseMode = ParseMode.MARKDOWN
)
}
}
}
—
bot {
// ...
dispatch {
command("greeting") {
val result = bot.sendMessage(
chatId = ChatId.fromId(message.chat.id),
text = "..."
)
result.fold({ /* ... */ }, { /* ... */ })
}
}
}
—
bot {
// ...
dispatch {
message(Filter.Sticker) {
// ...
}
newChatMembers {
newChatMembers.forEach {
// ...
}
}
}
}
Webhook
—
bot {
// ...
webhook {
url = ...
}
dispatch {
// ...
}
}.startWebhook()
embeddedServer(Netty, port = 8080) {
routing {
post("/...") {
val response = call.receiveText()
bot.processUpdate(response)
call.respond(HttpStatusCode.OK)
}
}
}.start(wait = true)
• Ktor
• ngrok
• webhook
• bot
Kotlin LINE SDK
—
https://github.com/line/line-bot-sdk-java
Spring Boot
—
—
dependencies {
// ...
implementation("...line-bot-spring-boot:$version")
// ...
}
Webhook
—
# src/main/resources/application.yml
line.bot:
channel-token: '...'
channel-secret: '...'
handler.path: /callback
• ngrok
• webhook
Bot
—
@LineMessageHandler
class LineBotController {
@EventMapping
fun handler(event: Event) {
// ...
}
}
EchoBot
—
@LineMessageHandler
class LineBotController {
@EventMapping
fun handleTextMessageEvent(
event: MessageEvent<TextMessageContent>
): Message
{
val originalMessageText = event.message.text
return TextMessage(originalMessageText)
}
}
— @LineMessageHandler
class LineBotController(
@Autowired val lineMessagingClient: LineMessagingClient
) {
private val log = ...
@EventMapping
fun handleTextMessageEvent(
event: MessageEvent<TextMessageContent>
) {
when(event.message.text) {
"ping" -> {
lineMessagingClient.replyMessage(
ReplyMessage(
event.replyToken,
listOf(TextMessage("Pong"))
)
).get()
}
}
}
}
— @LineMessageHandler
class LineBotController(
@Autowired val lineMessagingClient: LineMessagingClient
) {
private val log = ...
@EventMapping
fun handleStickerMessageEvent(
event: MessageEvent<StickerMessageContent>
) {
val apiResponse = lineMessagingClient.replyMessage(
ReplyMessage(
event.replyToken,
listOf(StickerMessage(
event.message.packageId,
event.message.stickerId)
)
)
).get()
}
}
Kotlin
—
• Kotlin
• Kotlin
•
• Kotlin Telegram Bot LINE Bot
—
• Telegram Bot
- https://github.com/shengyou/kotlin-telegram-bot-sample
• LINE Bot
- https://github.com/shengyou/kotlin-line-bot-sample
#
One more thing!
Kotlin
—
8:00
Line Telegram
https://tw.kotlin.tips
Tips
—
https://tw.intellij.tips
—
—
Coding
Kraftsman
(Shengyou Fan)
shengyou.fan@jetbrains.com
Q&A
—
Kotlin

用 Kotlin 打造讀書會小幫手