Unverified Commit b1ae7d84 authored by seykron's avatar seykron

Closes #6: adds fields to ask for different type of inputs

parent 9a998cae
......@@ -270,88 +270,110 @@
<build>
<finalName>tehanu</finalName>
<pluginManagement>
<plugins>
<!-- Deploy plugin -->
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- Release plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<releaseProfiles>release</releaseProfiles>
<localCheckout>true</localCheckout>
<pushChanges>false</pushChanges>
<mavenExecutorId>forked-path</mavenExecutorId>
<arguments>-Dgpg.passphrase=${gpg.passphrase} -Dgpg.keyname=${gpg.keyname}</arguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-api</artifactId>
<version>1.11.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<!-- Jar plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<excludes>
<!-- exclude any logback configuration from the jar -->
<exclude>**/logback.xml</exclude>
<!-- this line excludes env folder entirely from the jar -->
<exclude>**/application.conf</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Release plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<releaseProfiles>release</releaseProfiles>
<localCheckout>true</localCheckout>
<pushChanges>false</pushChanges>
<mavenExecutorId>forked-path</mavenExecutorId>
<arguments>-Dgpg.passphrase=${gpg.passphrase} -Dgpg.keyname=${gpg.keyname}</arguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-api</artifactId>
<version>1.11.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<extensions>true</extensions>
<!-- Jar plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<excludes>
<!-- exclude any logback configuration from the jar -->
<exclude>**/logback.xml</exclude>
<!-- this line excludes env folder entirely from the jar -->
<exclude>**/application.conf</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
<!-- Repositories -->
......
......@@ -189,60 +189,5 @@
</excludes>
</resource>
</resources>
<plugins>
<!-- Deploy plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Release plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
</plugin>
<!-- Jar plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
</plugins>
</build>
</project>
package be.rlab.tehanu.domain
import be.rlab.tehanu.domain.model.*
import be.rlab.tehanu.view.UserInput
import me.ivmg.telegram.entities.CallbackQuery
import me.ivmg.telegram.entities.ParseMode
import me.ivmg.telegram.entities.ReplyMarkup
......
......@@ -5,8 +5,6 @@ import be.rlab.tehanu.domain.model.Message
import be.rlab.tehanu.domain.model.User
/** Must be implemented in order to handle Telegram [Message]s.
*
* The [MessageContext] works as the scope to group messages.
*/
interface MessageListener {
......
package be.rlab.tehanu.domain
package be.rlab.tehanu.view
import be.rlab.tehanu.domain.MessageContext
import be.rlab.tehanu.domain.model.Message
import be.rlab.tehanu.domain.model.User
import be.rlab.tehanu.domain.model.input.Field
import be.rlab.tehanu.domain.model.input.FieldGroup
import be.rlab.tehanu.view.model.Field
import be.rlab.tehanu.view.model.FieldGroup
import me.ivmg.telegram.entities.CallbackQuery
import me.ivmg.telegram.entities.InlineKeyboardMarkup
import org.slf4j.Logger
......
package be.rlab.tehanu.view
/** Validation utilities for Telegram input.
*/
object Validators {
private val NUMBER_FORMAT: Regex = Regex("^\\d+\\.?\\d*$")
fun number(number: String) {
require(number.matches(NUMBER_FORMAT)) {
"El formato del número no es válido, tiene que tener el siguiente formato: 2077.42"
}
}
fun required(message: String? = null) {
require(false) {
message ?: "seleccioná un valor de la lista"
}
}
}
\ No newline at end of file
package be.rlab.tehanu.domain.model.input
package be.rlab.tehanu.view.model
import be.rlab.tehanu.domain.UserInput
import be.rlab.tehanu.view.UserInput
import be.rlab.tehanu.domain.model.User
import me.ivmg.telegram.entities.InlineKeyboardButton
import java.util.*
......
package be.rlab.tehanu.view.model
import be.rlab.tehanu.view.UserInput
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
/** Telegram field to ask for a [DateTime] type.
*/
fun UserInput.dateTime(
description: String,
format: String = "dd/MM/YYYY"
): Field = field(description) {
val dateTimeFormatter: DateTimeFormatter = DateTimeFormat.forPattern(format)
buildValue { rawValues ->
dateTimeFormatter.parseDateTime(rawValues.last().toString())
}
validator { value ->
try {
dateTimeFormatter.parseDateTime(value)
} catch (cause: IllegalArgumentException) {
throw IllegalArgumentException(
"El formato no es válido, el campo tiene que tener el siguiente formato: $format"
)
}
}
}
package be.rlab.tehanu.view.model
import be.rlab.tehanu.view.UserInput
/** Telegram field to ask for [Enum] types.
*
* It displays buttons with all values in the enumeration.
*/
inline fun<reified T : Enum<T>> UserInput.enumeration(
description: String,
validationMessage: String
): Field = field(description) {
keyboard {
enumValues<T>().forEach { value ->
button(value.name, value.name)
}
}
validator { selectedValue ->
require(enumValues<T>().any { value ->
value.name == selectedValue
}) { validationMessage }
}
}
package be.rlab.tehanu.domain.model.input
package be.rlab.tehanu.view.model
import be.rlab.tehanu.domain.PropertyBinder
import be.rlab.tehanu.domain.UserInput
import be.rlab.tehanu.view.UserInput
import be.rlab.tehanu.util.ObjectMapperFactory
import me.ivmg.telegram.entities.InlineKeyboardButton
import java.util.*
......
package be.rlab.tehanu.domain.model.input
package be.rlab.tehanu.view.model
import be.rlab.tehanu.domain.UserInput
import be.rlab.tehanu.view.UserInput
import be.rlab.tehanu.domain.model.LocationMessage
import be.rlab.tehanu.domain.model.Message
import be.rlab.tehanu.domain.model.TextMessage
......@@ -22,9 +22,10 @@ class FieldGroup<T>(
) {
companion object {
fun<T> new(userInput: UserInput): FieldGroup<T> = FieldGroup(
userInput = userInput
)
fun<T> new(userInput: UserInput): FieldGroup<T> =
FieldGroup(
userInput = userInput
)
}
private val logger: Logger = LoggerFactory.getLogger(FieldGroup::class.java)
......@@ -112,7 +113,8 @@ class FieldGroup<T>(
helpMessage: String,
callback: (Field.() -> Unit)? = null
): Field {
val newField: Field = Field.new(userInput, helpMessage)
val newField: Field =
Field.new(userInput, helpMessage)
callback?.let {
callback(newField)
......
package be.rlab.tehanu.domain.model.input
package be.rlab.tehanu.view.model
import be.rlab.tehanu.domain.UserInput
import be.rlab.tehanu.view.UserInput
import me.ivmg.telegram.entities.InlineKeyboardButton
import java.util.*
......@@ -44,7 +44,8 @@ class Keyboard(private val userInput: UserInput) {
value: Any,
callback: (Button.() -> Unit)? = null
) {
val button: Button = Button.new(userInput, title, value)
val button: Button =
Button.new(userInput, title, value)
callback?.let {
callback(button)
......
package be.rlab.tehanu.view.model
import be.rlab.tehanu.view.UserInput
import be.rlab.tehanu.view.Validators
/** Telegram field to ask for a valid number.
*/
fun UserInput.number(
description: String,
minValue: Number? = null,
maxValue: Number? = null
): Field = field(description) {
validator { value ->
Validators.number(value)
val resolvedValue: Double = parseValue(value)
minValue?.let {
require(
minValue.toDouble() > -1 && resolvedValue >= minValue.toDouble()
) { "el valor tiene que ser mayor o igual a $minValue" }
}
maxValue?.let {
require(
maxValue.toDouble() > -1 && resolvedValue <= maxValue.toDouble()
) { "el valor tiene que ser menor o igual a $maxValue" }
}
}
}
......@@ -160,60 +160,5 @@
</excludes>
</resource>
</resources>
<plugins>
<!-- Deploy plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Release plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
</plugin>
<!-- Jar plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
</plugins>
</build>
</project>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment