Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Данила Беляков authored and Данила Беляков committed Aug 18, 2024
1 parent 2f6bfc6 commit a764fef
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fun main() {
path = "/alice"
}
dispatch {
message({ message.session.new }) {
newSession {
response {
text = "Привет!"
}
Expand Down
5 changes: 5 additions & 0 deletions documentation/Первый_навык.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Прежде чем приступить к разработке навыка для Алисы, важно ознакомиться с [официальной документацией](https://yandex.ru/dev/dialogs/alice/doc/) и понять принцип работы навыков.

### Первые шаги

```kotlin
implementation("io.github.danbeldev:alice-ktx:{version}")
```

Этот кодовый фрагмент иллюстрирует создание и запуск навыка с использованием библиотеки для работы с сервером на базе [Ktor](https://ktor.io).
Навык обрабатывает сообщения и отвечает на них в зависимости от состояния сессии.

Expand Down
3 changes: 2 additions & 1 deletion examples/src/main/kotlin/com/github/examples/AudioPlayer.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.examples

import com.github.alice.ktx.dispatch
import com.github.alice.ktx.handlers.impl.newSession
import com.github.alice.ktx.handlers.message
import com.github.alice.ktx.models.audioPlayer.audioPlayer
import com.github.alice.ktx.models.response.response
Expand All @@ -15,7 +16,7 @@ fun main() {
path = "/alice"
}
dispatch {
message({ message.session.new }) {
newSession {
response {
shouldListen = false
audioPlayer {
Expand Down
3 changes: 2 additions & 1 deletion examples/src/main/kotlin/com/github/examples/Button.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.examples

import com.github.alice.ktx.dispatch
import com.github.alice.ktx.handlers.impl.newSession
import com.github.alice.ktx.handlers.message
import com.github.alice.ktx.models.button.button
import com.github.alice.ktx.models.response.response
Expand All @@ -21,7 +22,7 @@ fun main() {
path = "/alice"
}
dispatch {
message({ message.session.new }) {
newSession {
response {
text = "Выберите тип"
SchedulesType.entries.forEach {
Expand Down
3 changes: 2 additions & 1 deletion examples/src/main/kotlin/com/github/examples/Echo.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.examples

import com.github.alice.ktx.dispatch
import com.github.alice.ktx.handlers.impl.newSession
import com.github.alice.ktx.handlers.message
import com.github.alice.ktx.models.response.response
import com.github.alice.ktx.server.impl.ktorWebServer
Expand All @@ -14,7 +15,7 @@ fun main() {
path = "/alice"
}
dispatch {
message({ message.session.new }) {
newSession {
response {
text = "Привет!"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.github.examples

import com.github.alice.ktx.dispatch
import com.github.alice.ktx.handlers.impl.newSession
import com.github.alice.ktx.handlers.message
import com.github.alice.ktx.models.button.button
import com.github.alice.ktx.models.response.response
import com.github.alice.ktx.server.impl.ktorWebServer
import com.github.alice.ktx.skill
import com.github.alice.ktx.state.FSMContext
import com.github.alice.ktx.state.KotlinxSerializationFSMContext
import com.github.alice.ktx.state.impl.KotlinxSerializationFSMContext
import kotlinx.serialization.Serializable

@Serializable
Expand Down Expand Up @@ -36,13 +37,13 @@ fun main() {
path = "/alice"
}
dispatch {
message({ message.session.new }) {
newSession {
state.setState(UserState.SET_NAME.name)
response {
text = "Привет! Как тебя зовут?"
}
}
message({ state == UserState.SET_NAME.name }) {
message({ context.getState() == UserState.SET_NAME.name }) {
val user = User(username = message.request.originalUtterance!!)
state.setTypedData("user" to user, clazz = User::class)
state.setState(UserState.GET_NAME.name)
Expand Down
13 changes: 7 additions & 6 deletions examples/src/main/kotlin/com/github/examples/FsmForm.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.examples

import com.github.alice.ktx.dispatch
import com.github.alice.ktx.handlers.impl.newSession
import com.github.alice.ktx.handlers.message
import com.github.alice.ktx.models.button.button
import com.github.alice.ktx.models.response.response
Expand All @@ -21,7 +22,7 @@ fun main() {
path = "/alice"
}
dispatch {
message({ message.session.new }) {
newSession {
state.setState(FormState.NAME.name)
response {
text = "Привет! Как тебя зовут?"
Expand All @@ -36,7 +37,7 @@ fun main() {
}
}

message({ state == FormState.NAME.name }) {
message({ context.getState() == FormState.NAME.name }) {
val name = message.request.originalUtterance.toString()
state.updateData("name" to name)
state.setState(FormState.LINK_SKILLS.name)
Expand All @@ -51,7 +52,7 @@ fun main() {
}
}

message({ state == FormState.LINK_SKILLS.name && message.request.command == "нет" }) {
message({ context.getState() == FormState.LINK_SKILLS.name && message.request.command == "нет" }) {
val data = state.getData()
state.clear()
response {
Expand All @@ -60,14 +61,14 @@ fun main() {
}
}

message({ state == FormState.LINK_SKILLS.name && message.request.command == "да" }) {
message({ context.getState() == FormState.LINK_SKILLS.name && message.request.command == "да" }) {
state.setState(FormState.DEVICE.name)
response {
text = "Класс! Мне тоже!\nЧерез какое устройство ты обычно их используешь?"
}
}

message({ state == FormState.LINK_SKILLS.name }) {
message({ context.getState() == FormState.LINK_SKILLS.name }) {
response {
text = "Не могу понять тебя... Можешь повторить, пожалуйста?"
button {
Expand All @@ -79,7 +80,7 @@ fun main() {
}
}

message({ state == FormState.DEVICE.name }) {
message({ context.getState() == FormState.DEVICE.name }) {
val device = message.request.originalUtterance.toString()
state.updateData("device" to device)
val data = state.getData()
Expand Down
36 changes: 35 additions & 1 deletion examples/src/main/kotlin/com/github/examples/Handlers.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
package com.github.examples

class Handlers {
import com.github.alice.ktx.dispatch
import com.github.alice.ktx.handlers.impl.buttonPressed
import com.github.alice.ktx.handlers.impl.newSession
import com.github.alice.ktx.models.button.button
import com.github.alice.ktx.models.response.response
import com.github.alice.ktx.server.impl.ktorWebServer
import com.github.alice.ktx.skill

/**
* Демонстрация новых хендлеров [newSession], [buttonPressed]
* */
fun main() {
skill {
webServer = ktorWebServer {
port = 8080
path = "/alice"
}
dispatch {
newSession {
response {
text = "Start"
button {
title = "Test"
payload = mapOf("test" to "1")
}
}
}

buttonPressed({ payload["test"] == "1" }) {
response {
text = "Click"
}
}
}
}.run()
}
9 changes: 5 additions & 4 deletions examples/src/main/kotlin/com/github/examples/Info.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.examples

import com.github.alice.ktx.dispatch
import com.github.alice.ktx.handlers.impl.newSession
import com.github.alice.ktx.handlers.message
import com.github.alice.ktx.models.response.response
import com.github.alice.ktx.server.impl.ktorWebServer
Expand All @@ -21,29 +22,29 @@ fun main() {
path = "/alice"
}
dispatch {
message({ message.session.new }) {
newSession {
state.setState(InfoState.SET_NAME.name)
response {
text = "Добро пожаловать в навык, как вас зовут?"
}
}
message({ state == InfoState.SET_NAME.name }) {
message({ context.getState() == InfoState.SET_NAME.name }) {
val username = message.request.originalUtterance.toString()
state.updateData("name" to username)
state.setState(InfoState.SET_AGE.name)
response {
text = "Рад познакомиться $username, сколько вам лет?"
}
}
message({ state == InfoState.SET_AGE.name }) {
message({ context.getState() == InfoState.SET_AGE.name }) {
val age = message.request.originalUtterance.toString()
state.updateData("age" to age)
state.setState(InfoState.SET_INFO.name)
response {
text = "Супер, расскажите о себе"
}
}
message({state == InfoState.SET_INFO.name}) {
message({context.getState() == InfoState.SET_INFO.name}) {
val info = message.request.originalUtterance.toString()
val data = state.getData()
state.clear()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.examples.cities

import com.github.alice.ktx.dispatch
import com.github.alice.ktx.handlers.impl.newSession
import com.github.alice.ktx.handlers.message
import com.github.alice.ktx.middleware.outerMiddleware
import com.github.alice.ktx.models.response.response
Expand Down Expand Up @@ -33,7 +34,7 @@ fun main() {
null
}

message({ message.session.new }) {
newSession {
val userId = message.session.user!!.userId
val isUserExisting = citiesGameService.isUserExisting(userId)
if(!isUserExisting) {
Expand All @@ -44,7 +45,7 @@ fun main() {
}
}

message({ state == RegisterUserState.INPUT_FIST_NAME.name }) {
message({ context.getState() == RegisterUserState.INPUT_FIST_NAME.name }) {
val userId = message.session.user!!.userId
val firstName = message.request.originalUtterance!!
citiesGameService.createUser(id = userId, firstName = firstName)
Expand Down

0 comments on commit a764fef

Please sign in to comment.