-
-
Notifications
You must be signed in to change notification settings - Fork 139
Add SMS message skill #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tylxr59
wants to merge
2
commits into
Stypox:master
Choose a base branch
from
tylxr59:add-sms-skill
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
app/src/main/kotlin/org/stypox/dicio/skills/sms/AskMessageOutput.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package org.stypox.dicio.skills.sms | ||
|
|
||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.Spacer | ||
| import androidx.compose.foundation.layout.height | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.unit.dp | ||
| import org.dicio.skill.context.SkillContext | ||
| import org.dicio.skill.skill.AlwaysBestScore | ||
| import org.dicio.skill.skill.AlwaysWorstScore | ||
| import org.dicio.skill.skill.InteractionPlan | ||
| import org.dicio.skill.skill.Score | ||
| import org.dicio.skill.skill.Skill | ||
| import org.dicio.skill.skill.SkillOutput | ||
| import org.dicio.skill.skill.Specificity | ||
| import org.stypox.dicio.R | ||
| import org.stypox.dicio.io.graphical.Body | ||
| import org.stypox.dicio.io.graphical.Headline | ||
| import org.stypox.dicio.util.getString | ||
|
|
||
| class AskMessageOutput( | ||
| private val name: String, | ||
| private val number: String | ||
| ) : SkillOutput { | ||
| override fun getSpeechOutput(ctx: SkillContext): String = | ||
| ctx.getString(R.string.skill_sms_what_message) | ||
|
|
||
| override fun getInteractionPlan(ctx: SkillContext): InteractionPlan { | ||
| val messageSkill = object : Skill<String>(SmsInfo, Specificity.HIGH) { | ||
| override fun score( | ||
| ctx: SkillContext, | ||
| input: String | ||
| ): Pair<Score, String> { | ||
| val trimmedInput = input.trim() | ||
| return Pair( | ||
| if (trimmedInput.isEmpty()) AlwaysWorstScore else AlwaysBestScore, | ||
| trimmedInput | ||
| ) | ||
| } | ||
|
|
||
| override suspend fun generateOutput( | ||
| ctx: SkillContext, | ||
| inputData: String | ||
| ): SkillOutput { | ||
| return ConfirmSmsOutput(name, number, inputData) | ||
| } | ||
| } | ||
|
|
||
| return InteractionPlan.StartSubInteraction( | ||
| reopenMicrophone = true, | ||
| nextSkills = listOf(messageSkill), | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| override fun GraphicalOutput(ctx: SkillContext) { | ||
| Column { | ||
| Headline(text = getSpeechOutput(ctx)) | ||
| Spacer(modifier = Modifier.height(4.dp)) | ||
| Body(text = "$name ($number)") | ||
| } | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
app/src/main/kotlin/org/stypox/dicio/skills/sms/ConfirmSmsOutput.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package org.stypox.dicio.skills.sms | ||
|
|
||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.Spacer | ||
| import androidx.compose.foundation.layout.height | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.unit.dp | ||
| import org.dicio.skill.context.SkillContext | ||
| import org.dicio.skill.skill.InteractionPlan | ||
| import org.dicio.skill.skill.SkillOutput | ||
| import org.stypox.dicio.R | ||
| import org.stypox.dicio.io.graphical.Body | ||
| import org.stypox.dicio.io.graphical.Headline | ||
| import org.stypox.dicio.sentences.Sentences | ||
| import org.stypox.dicio.util.RecognizeYesNoSkill | ||
| import org.stypox.dicio.util.getString | ||
|
|
||
| class ConfirmSmsOutput( | ||
| private val name: String, | ||
| private val number: String, | ||
| private val message: String | ||
| ) : SkillOutput { | ||
| override fun getSpeechOutput(ctx: SkillContext): String = | ||
| ctx.getString(R.string.skill_sms_confirm, message, name) | ||
|
|
||
| override fun getInteractionPlan(ctx: SkillContext): InteractionPlan { | ||
| val yesNoSentences = Sentences.UtilYesNo[ctx.sentencesLanguage]!! | ||
| val confirmYesNoSkill = object : RecognizeYesNoSkill(SmsInfo, yesNoSentences) { | ||
| override suspend fun generateOutput( | ||
| ctx: SkillContext, | ||
| inputData: Boolean | ||
| ): SkillOutput { | ||
| return if (inputData) { | ||
| SmsSkill.sendSms(number, message) | ||
| SentSmsOutput(name, number, message) | ||
| } else { | ||
| SentSmsOutput(null, null, null) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return InteractionPlan.ReplaceSubInteraction( | ||
| reopenMicrophone = true, | ||
| nextSkills = listOf(confirmYesNoSkill), | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| override fun GraphicalOutput(ctx: SkillContext) { | ||
| Column { | ||
| Headline(text = getSpeechOutput(ctx)) | ||
| Spacer(modifier = Modifier.height(4.dp)) | ||
| Body(text = "$name ($number)") | ||
| } | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
app/src/main/kotlin/org/stypox/dicio/skills/sms/SentSmsOutput.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package org.stypox.dicio.skills.sms | ||
|
|
||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.Spacer | ||
| import androidx.compose.foundation.layout.height | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.unit.dp | ||
| import org.dicio.skill.context.SkillContext | ||
| import org.dicio.skill.skill.SkillOutput | ||
| import org.stypox.dicio.R | ||
| import org.stypox.dicio.io.graphical.Body | ||
| import org.stypox.dicio.io.graphical.Headline | ||
| import org.stypox.dicio.util.getString | ||
|
|
||
| class SentSmsOutput( | ||
| private val name: String?, | ||
| private val number: String?, | ||
| private val message: String? | ||
| ) : SkillOutput { | ||
| override fun getSpeechOutput(ctx: SkillContext): String = if (name == null) { | ||
| ctx.getString(R.string.skill_sms_not_sending) | ||
| } else { | ||
| "" // do not speak anything since the message was sent | ||
| } | ||
|
|
||
| @Composable | ||
| override fun GraphicalOutput(ctx: SkillContext) { | ||
| if (name == null) { | ||
| Headline(text = stringResource(R.string.skill_sms_not_sending)) | ||
| } else { | ||
| Column { | ||
| Headline(text = stringResource(R.string.skill_sms_sent, name)) | ||
| Spacer(modifier = Modifier.height(4.dp)) | ||
| Body(text = number ?: "") | ||
| if (message != null) { | ||
| Spacer(modifier = Modifier.height(8.dp)) | ||
| Body(text = "\"$message\"") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
app/src/main/kotlin/org/stypox/dicio/skills/sms/SmsContactChooserIndex.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package org.stypox.dicio.skills.sms | ||
|
|
||
| import org.dicio.numbers.unit.Number | ||
| import org.dicio.skill.context.SkillContext | ||
| import org.dicio.skill.skill.AlwaysBestScore | ||
| import org.dicio.skill.skill.AlwaysWorstScore | ||
| import org.dicio.skill.skill.Score | ||
| import org.dicio.skill.skill.Skill | ||
| import org.dicio.skill.skill.SkillOutput | ||
| import org.dicio.skill.skill.Specificity | ||
|
|
||
| class SmsContactChooserIndex internal constructor( | ||
| private val contacts: List<Pair<String, String>>, | ||
| private val messageText: String? | ||
| ) : Skill<Int>(SmsInfo, Specificity.HIGH) { | ||
|
|
||
| override fun score( | ||
| ctx: SkillContext, | ||
| input: String | ||
| ): Pair<Score, Int> { | ||
| val index = ctx.parserFormatter!! | ||
| .extractNumber(input) | ||
| .preferOrdinal(true) | ||
| .mixedWithText | ||
| .asSequence() | ||
| .filter { obj -> (obj as? Number)?.isInteger == true } | ||
| .map { obj -> (obj as Number).integerValue().toInt() } | ||
| .firstOrNull() ?: 0 | ||
| return Pair( | ||
| if (index <= 0 || index > contacts.size) AlwaysWorstScore else AlwaysBestScore, | ||
| index | ||
| ) | ||
| } | ||
|
|
||
| override suspend fun generateOutput(ctx: SkillContext, inputData: Int): SkillOutput { | ||
| if (inputData > 0 && inputData <= contacts.size) { | ||
| val contact = contacts[inputData - 1] | ||
| return if (messageText == null) { | ||
| AskMessageOutput(contact.first, contact.second) | ||
| } else { | ||
| ConfirmSmsOutput(contact.first, contact.second, messageText) | ||
| } | ||
| } else { | ||
| // impossible situation | ||
| return SentSmsOutput(null, null, null) | ||
| } | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
app/src/main/kotlin/org/stypox/dicio/skills/sms/SmsContactChooserName.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package org.stypox.dicio.skills.sms | ||
|
|
||
| import org.dicio.skill.context.SkillContext | ||
| import org.dicio.skill.skill.AlwaysBestScore | ||
| import org.dicio.skill.skill.AlwaysWorstScore | ||
| import org.dicio.skill.skill.Score | ||
| import org.dicio.skill.skill.Skill | ||
| import org.dicio.skill.skill.SkillOutput | ||
| import org.dicio.skill.skill.Specificity | ||
| import org.stypox.dicio.util.StringUtils | ||
|
|
||
| class SmsContactChooserName internal constructor( | ||
| private val contacts: List<Pair<String, String>>, | ||
| private val messageText: String? | ||
| ) : Skill<Pair<String, String>?>(SmsInfo, Specificity.LOW) { | ||
|
|
||
| override fun score( | ||
| ctx: SkillContext, | ||
| input: String | ||
| ): Pair<Score, Pair<String, String>?> { | ||
| val trimmedInput = input.trim { it <= ' ' } | ||
|
|
||
| val bestContact = contacts | ||
| .map { nameNumberPair -> | ||
| Pair( | ||
| nameNumberPair, | ||
| StringUtils.contactStringDistance(trimmedInput, nameNumberPair.first) | ||
| ) | ||
| } | ||
| .filter { pair -> pair.second < -7 } | ||
| .minByOrNull { a -> a.second } | ||
| ?.first | ||
|
|
||
| return Pair( | ||
| if (bestContact == null) AlwaysWorstScore else AlwaysBestScore, | ||
| bestContact | ||
| ) | ||
| } | ||
|
|
||
| override suspend fun generateOutput(ctx: SkillContext, inputData: Pair<String, String>?): SkillOutput { | ||
| return inputData?.let { | ||
| if (messageText == null) { | ||
| AskMessageOutput(it.first, it.second) | ||
| } else { | ||
| ConfirmSmsOutput(it.first, it.second, messageText) | ||
| } | ||
| } | ||
| // impossible situation | ||
| ?: SentSmsOutput(null, null, null) | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
app/src/main/kotlin/org/stypox/dicio/skills/sms/SmsInfo.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package org.stypox.dicio.skills.sms | ||
|
|
||
| import android.content.Context | ||
| import androidx.compose.material.icons.Icons | ||
| import androidx.compose.material.icons.filled.Message | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.graphics.vector.rememberVectorPainter | ||
| import org.dicio.skill.context.SkillContext | ||
| import org.dicio.skill.skill.Permission | ||
| import org.dicio.skill.skill.Skill | ||
| import org.dicio.skill.skill.SkillInfo | ||
| import org.stypox.dicio.R | ||
| import org.stypox.dicio.sentences.Sentences | ||
| import org.stypox.dicio.util.PERMISSION_READ_CONTACTS | ||
| import org.stypox.dicio.util.PERMISSION_SEND_SMS | ||
|
|
||
| object SmsInfo : SkillInfo("sms") { | ||
| override fun name(context: Context) = | ||
| context.getString(R.string.skill_name_sms) | ||
|
|
||
| override fun sentenceExample(context: Context) = | ||
| context.getString(R.string.skill_sentence_example_sms) | ||
|
|
||
| @Composable | ||
| override fun icon() = | ||
| rememberVectorPainter(Icons.Default.Message) | ||
|
|
||
| override val neededPermissions: List<Permission> | ||
| = listOf(PERMISSION_READ_CONTACTS, PERMISSION_SEND_SMS) | ||
|
|
||
| override fun isAvailable(ctx: SkillContext): Boolean { | ||
| return Sentences.Sms[ctx.sentencesLanguage] != null && | ||
| Sentences.UtilYesNo[ctx.sentencesLanguage] != null | ||
| } | ||
|
|
||
| override fun build(ctx: SkillContext): Skill<*> { | ||
| return SmsSkill(SmsInfo, Sentences.Sms[ctx.sentencesLanguage]!!) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Saying something while a call is starting is bad, but saying something while a message is being sent is fine