-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
190 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
Binary file not shown.
This file contains 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip | ||
networkTimeout=10000 | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains 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 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 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
56 changes: 56 additions & 0 deletions
56
src/main/java/com/cadiducho/bot/modules/refranero/Refranero.java
This file contains 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,56 @@ | ||
package com.cadiducho.bot.modules.refranero; | ||
|
||
import com.cadiducho.telegrambotapi.Update; | ||
import com.cadiducho.telegrambotapi.exception.TelegramException; | ||
import com.cadiducho.telegrambotapi.util.MoshiProvider; | ||
import com.cadiducho.telegrambotapi.Chat; | ||
import com.cadiducho.telegrambotapi.Message; | ||
import com.cadiducho.telegrambotapi.User; | ||
import com.cadiducho.zincite.ZinciteBot; | ||
import com.cadiducho.zincite.api.command.CommandManager; | ||
import com.cadiducho.zincite.api.module.ModuleInfo; | ||
import com.cadiducho.zincite.api.module.ZinciteModule; | ||
import com.cadiducho.zincite.api.command.BotCommand; | ||
import com.cadiducho.zincite.api.command.CommandContext; | ||
import com.cadiducho.zincite.api.command.CommandInfo; | ||
import com.squareup.moshi.JsonAdapter; | ||
import com.squareup.moshi.Moshi; | ||
import com.squareup.moshi.Types; | ||
import lombok.Getter; | ||
import lombok.extern.java.Log; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.*; | ||
import java.util.logging.Level; | ||
|
||
@Log | ||
@ModuleInfo(name = "refranero", description = "Obtiene un refran del precioso idioma que es el Castellano") | ||
public class Refranero implements ZinciteModule { | ||
|
||
|
||
@Override | ||
public void onLoad() { | ||
log.info("Cargando módulo refranero"); | ||
CommandManager commandManager = ZinciteBot.getInstance().getCommandManager(); | ||
commandManager.register(new RefranCMD()); | ||
} | ||
|
||
@CommandInfo( | ||
aliases = {"/refran", "/refranero", "/sabiduria"}, | ||
description = "Obtiene un refran del precioso idioma que es el Castellano" | ||
) | ||
class RefranCMD implements BotCommand { | ||
private final Random rand = new Random(); | ||
|
||
@Override | ||
public void execute(Chat chat, User user, CommandContext commandContext, Integer integer, Message message, Instant instant) throws TelegramException { | ||
List<String> respuestas = Respuestas.getInstance().getRespuestas(); | ||
String reply = respuestas.get(rand.nextInt(respuestas.size())); | ||
getBot().sendMessage(chat.getId(), reply); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/cadiducho/bot/modules/refranero/Respuestas.java
This file contains 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,65 @@ | ||
package com.cadiducho.bot.modules.refranero; | ||
|
||
import com.cadiducho.telegrambotapi.util.MoshiProvider; | ||
import com.squareup.moshi.JsonAdapter; | ||
import com.squareup.moshi.Types; | ||
import lombok.Getter; | ||
import lombok.extern.java.Log; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
|
||
@Log | ||
final class Respuestas { | ||
|
||
private static final String URL_RESPUESTAS = "https://raw.githubusercontent.com/Alonsistas/33/main/refranes.json"; | ||
|
||
private static final OkHttpClient client = new OkHttpClient(); | ||
private static final JsonAdapter<List<String>> adapter = MoshiProvider.getMoshi().adapter(Types.newParameterizedType(List.class, String.class)); | ||
|
||
@Getter | ||
private List<String> respuestas; | ||
|
||
private static Respuestas INSTANCE; | ||
|
||
private Respuestas() { | ||
if (INSTANCE != null) throw new UnsupportedOperationException("Cannot create singleton class twice"); | ||
INSTANCE = this; | ||
try { | ||
respuestas = obtenerRespuestas(); | ||
} catch (IOException e) { | ||
log.log(Level.WARNING, "Error obteniendo respuestas del módulo refranero", e); | ||
respuestas = new ArrayList<>(List.of("no hay refranes :(")); | ||
} | ||
|
||
} | ||
|
||
private static List<String> obtenerRespuestas() throws IOException { | ||
List<String> lista = new ArrayList<>(); | ||
Request request = new Request.Builder() | ||
.url(URL_RESPUESTAS) | ||
.build(); | ||
|
||
try (Response response = client.newCall(request).execute()) { | ||
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); | ||
lista = adapter.fromJson(response.body().source()); | ||
} | ||
return lista; | ||
} | ||
|
||
public void refrescar() throws IOException { | ||
respuestas = obtenerRespuestas(); | ||
} | ||
|
||
public static Respuestas getInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = new Respuestas(); | ||
} | ||
return INSTANCE; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.