diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index da082db..57c71a4 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -13,7 +13,6 @@ LongParameterList:Twitter.kt$Twitter.Companion$( consumerKey: String?, consumerSecret: String?, token: String?, tokenSecret: String?, handle: String?, message: String, isDm: Boolean ) MagicNumber:ChatGpt.kt$ChatGpt$400 MagicNumber:ChatGpt.kt$ChatGpt.Companion$200 - MagicNumber:ChatGpt.kt$ChatGpt.Companion$429 MagicNumber:Comment.kt$Comment$3 MagicNumber:CryptoPrices.kt$CryptoPrices$10 MagicNumber:CurrencyConverter.kt$CurrencyConverter$11 diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt index d62d364..e2bccb0 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt @@ -50,16 +50,16 @@ import java.net.http.HttpResponse class ChatGpt : AbstractModule() { private val logger: Logger = LoggerFactory.getLogger(ChatGpt::class.java) - override val name = CHATGPT_NAME + override val name = "ChatGPT" override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { if (args.isNotBlank()) { try { - val answer = chat(args.trim(), properties[API_KEY_PROP], properties[MAX_TOKENS_PROP]!!.toInt()) + val answer = chat(args.trim(), properties[CHATGPT_API_KEY], properties[CHATGPT_MAX_TOKENS]!!.toInt()) if (answer.isNotBlank()) { event.sendMessage(WordUtils.wrap(answer, 400)) } else { - event.respond("$name is stumped.") + event.respond("ChatGPT is stumped.") } } catch (e: ModuleException) { if (logger.isWarnEnabled) logger.warn(e.debugMessage, e) @@ -67,7 +67,7 @@ class ChatGpt : AbstractModule() { event.respond(it) } } catch (e: NumberFormatException) { - if (logger.isErrorEnabled) logger.error("Invalid $MAX_TOKENS_PROP property.", e) + if (logger.isErrorEnabled) logger.error("Invalid $CHATGPT_MAX_TOKENS property.", e) event.respond("The $name module is misconfigured.") } } else { @@ -77,26 +77,20 @@ class ChatGpt : AbstractModule() { companion object { /** - * The service name. + * The ChatGPT API Key property. */ - const val CHATGPT_NAME = "ChatGPT" + const val CHATGPT_API_KEY = "chatgpt-api-key" /** - * The API Key property. + * The ChatGPT max tokens property. */ - const val API_KEY_PROP = "chatgpt-api-key" - - /** - * The max tokens property. - */ - const val MAX_TOKENS_PROP = "chatgpt-max-tokens" - - // ChatGPT API URL - private const val API_URL = "https://api.openai.com/v1/completions" + const val CHATGPT_MAX_TOKENS = "chatgpt-max-tokens" // ChatGPT command private const val CHATGPT_CMD = "chatgpt" + // ChatGPT API URL + private const val API_URL = "https://api.openai.com/v1/completions" @JvmStatic @Throws(ModuleException::class) @@ -130,28 +124,23 @@ class ChatGpt : AbstractModule() { return choices.getJSONObject(0).getString("text").trim() } catch (e: JSONException) { throw ModuleException( - "$CHATGPT_CMD($query): JSON", - "A JSON error has occurred while conversing with $CHATGPT_NAME.", + "chatgpt($query): JSON", + "A JSON error has occurred while conversing with ChatGPT.", e ) } } else { - if (response.statusCode() == 429) { - throw ModuleException("$CHATGPT_CMD($query): Rate limit reached", - "Rate limit reached. Please try again later.") - } else { - throw IOException("HTTP Status Code: " + response.statusCode()) - } + throw IOException("Status Code: " + response.statusCode()) } } catch (e: IOException) { throw ModuleException( - "$CHATGPT_CMD($query): IO", - "An IO error has occurred while conversing with $CHATGPT_NAME.", + "chatgpt($query): IO", + "An IO error has occurred while conversing with ChatGPT.", e ) } } else { - throw ModuleException("$CHATGPT_CMD($query)", "No $CHATGPT_NAME API key specified.") + throw ModuleException("chatgpt($query)", "No ChatGPT API key specified.") } } } @@ -159,12 +148,12 @@ class ChatGpt : AbstractModule() { init { commands.add(CHATGPT_CMD) with(help) { - add("To get answers from $name:") + add("To get answers from ChatGPT:") add(Utils.helpFormat("%c $CHATGPT_CMD ")) add("For example:") add(Utils.helpFormat("%c $CHATGPT_CMD explain quantum computing in simple terms")) add(Utils.helpFormat("%c $CHATGPT_CMD how do I make an HTTP request in Javascript?")) } - initProperties(API_KEY_PROP, MAX_TOKENS_PROP) + initProperties(CHATGPT_API_KEY, CHATGPT_MAX_TOKENS) } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt index 05647bd..66a33ae 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt @@ -65,7 +65,7 @@ class CryptoPrices : AbstractModule() { } val debugMessage = "crypto($cmd $args)" - if (args == CODES_KEYWORD) { + if (args == CURRENCY_CODES_KEYWORD) { event.sendMessage("The supported currencies are:") event.sendList(ArrayList(CURRENCIES.keys), 10, isIndent = true) } else if (args.matches("\\w+( [a-zA-Z]{3}+)?".toRegex())) { @@ -100,7 +100,7 @@ class CryptoPrices : AbstractModule() { private val CURRENCIES: MutableMap = mutableMapOf() // Currency codes keyword - private const val CODES_KEYWORD = "codes" + private const val CURRENCY_CODES_KEYWORD = "codes" /** * Get current market price. @@ -153,7 +153,7 @@ class CryptoPrices : AbstractModule() { add(helpFormat("%c $CRYPTO_CMD ETH EUR")) add(helpFormat("%c $CRYPTO_CMD ETH2 GPB")) add("To list the supported currencies:") - add(helpFormat("%c $CRYPTO_CMD $CODES_KEYWORD")) + add(helpFormat("%c $CRYPTO_CMD $CURRENCY_CODES_KEYWORD")) } loadCurrencies() } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt index 02af2b6..76451db 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt @@ -67,8 +67,8 @@ class GoogleSearch : AbstractModule() { try { val results = searchGoogle( args, - properties[API_KEY_PROP], - properties[CSE_KEY_PROP], + properties[GOOGLE_API_KEY_PROP], + properties[GOOGLE_CSE_KEY_PROP], event.user.nick ) for (msg in results) { @@ -91,10 +91,10 @@ class GoogleSearch : AbstractModule() { companion object { // Google API Key property - const val API_KEY_PROP = "google-api-key" + const val GOOGLE_API_KEY_PROP = "google-api-key" // Google Custom Search Engine ID property - const val CSE_KEY_PROP = "google-cse-cx" + const val GOOGLE_CSE_KEY_PROP = "google-cse-cx" // Google command private const val GOOGLE_CMD = "google" @@ -158,6 +158,6 @@ class GoogleSearch : AbstractModule() { commands.add(GOOGLE_CMD) help.add("To search Google:") help.add(helpFormat("%c $GOOGLE_CMD ")) - initProperties(API_KEY_PROP, CSE_KEY_PROP) + initProperties(GOOGLE_API_KEY_PROP, GOOGLE_CSE_KEY_PROP) } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt index 6df2317..ffca08b 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt @@ -63,7 +63,7 @@ class StockQuote : AbstractModule() { override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { if (args.isNotBlank()) { try { - val messages = getQuote(args, properties[API_KEY_PROP]) + val messages = getQuote(args, properties[ALPHAVANTAGE_API_KEY_PROP]) for (msg in messages) { event.sendMessage(channel, msg) } @@ -80,17 +80,17 @@ class StockQuote : AbstractModule() { companion object { /** - * The API property key. + * The Alpha Advantage property key. */ - const val API_KEY_PROP = "alphavantage-api-key" + const val ALPHAVANTAGE_API_KEY_PROP = "alphavantage-api-key" /** * The Invalid Symbol error string. */ const val INVALID_SYMBOL = "Invalid symbol." - // API URL - private const val API_URL = "https://www.alphavantage.co/query?function=" + // Alpha Advantage URL + private const val ALPHAVANTAGE_URL = "https://www.alphavantage.co/query?function=" // Quote command private const val STOCK_CMD = "stock" @@ -145,7 +145,7 @@ class StockQuote : AbstractModule() { with(messages) { // Search for symbol/keywords response = URL( - "${API_URL}SYMBOL_SEARCH&keywords=" + symbol.encodeUrl() + "&apikey=" + "${ALPHAVANTAGE_URL}SYMBOL_SEARCH&keywords=" + symbol.encodeUrl() + "&apikey=" + apiKey.encodeUrl() ).reader().body var json = getJsonResponse(response, debugMessage) @@ -157,7 +157,7 @@ class StockQuote : AbstractModule() { // Get quote for symbol response = URL( - "${API_URL}GLOBAL_QUOTE&symbol=" + "${ALPHAVANTAGE_URL}GLOBAL_QUOTE&symbol=" + symbolInfo.getString("1. symbol").encodeUrl() + "&apikey=" + apiKey.encodeUrl() ).reader().body @@ -232,6 +232,6 @@ class StockQuote : AbstractModule() { commands.add(STOCK_CMD) help.add("To retrieve a stock quote:") help.add(helpFormat("%c $STOCK_CMD ")) - initProperties(API_KEY_PROP) + initProperties(ALPHAVANTAGE_API_KEY_PROP) } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt index ddb82b2..4fc3770 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt @@ -84,7 +84,7 @@ class Twitter : SocialModule() { const val TOKEN_PROP = "twitter-token" const val TOKEN_SECRET_PROP = "twitter-tokenSecret" - // Twitter commands + // Twitter command private const val TWITTER_CMD = "twitter" private const val TWEET_CMD = "tweet" @@ -118,7 +118,7 @@ class Twitter : SocialModule() { dm.text } } catch (e: TwitterException) { - throw ModuleException("tweet($message)", "An error has occurred: ${e.message}", e) + throw ModuleException("twitterPost($message)", "An error has occurred: ${e.message}", e) } } } @@ -126,7 +126,7 @@ class Twitter : SocialModule() { init { commands.add(TWITTER_CMD) commands.add(TWEET_CMD) - help.add("To $TWEET_CMD on $name:") + help.add("To tweet on Twitter:") help.add(helpFormat("%c $TWEET_CMD ")) properties[AUTO_POST_PROP] = "false" initProperties(CONSUMER_KEY_PROP, CONSUMER_SECRET_PROP, HANDLE_PROP, TOKEN_PROP, TOKEN_SECRET_PROP) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt index ec963fe..5c5ae67 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt @@ -65,7 +65,7 @@ class Weather2 : AbstractModule() { override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { if (args.isNotBlank()) { try { - val messages = getWeather(args, properties[API_KEY_PROP]) + val messages = getWeather(args, properties[OWM_API_KEY_PROP]) if (messages[0].isError) { helpResponse(event) } else { @@ -88,7 +88,7 @@ class Weather2 : AbstractModule() { /** * The OpenWeatherMap API Key property. */ - const val API_KEY_PROP = "owm-api-key" + const val OWM_API_KEY_PROP = "owm-api-key" // Weather command private const val WEATHER_CMD = "weather" @@ -246,6 +246,6 @@ class Weather2 : AbstractModule() { add(helpFormat("%c $WEATHER_CMD paris, fr")) add("The default ISO 3166 country code is ${"US".bold()}. Zip codes supported in most countries.") } - initProperties(API_KEY_PROP) + initProperties(OWM_API_KEY_PROP) } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/WolframAlpha.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/WolframAlpha.kt index 204ad17..56d3a3d 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/WolframAlpha.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/WolframAlpha.kt @@ -66,9 +66,9 @@ class WolframAlpha : AbstractModule() { units = if (query.size == 2) { getUnits(query[1].trim()) } else { - getUnits(properties[UNITS_PROP]) + getUnits(properties[WOLFRAM_UNITS_PROP]) }, - appId = properties[APPID_KEY_PROP] + appId = properties[WOLFRAM_APPID_KEY] ) ) } catch (e: ModuleException) { @@ -86,13 +86,12 @@ class WolframAlpha : AbstractModule() { /** * The Wolfram Alpha API Key property. */ - const val APPID_KEY_PROP = "wolfram-appid" + const val WOLFRAM_APPID_KEY = "wolfram-appid" /** * The Wolfram units properties */ - const val UNITS_PROP = "wolfram-units" - + const val WOLFRAM_UNITS_PROP = "wolfram-units" const val METRIC = "metric" const val IMPERIAL = "imperial" @@ -138,6 +137,6 @@ class WolframAlpha : AbstractModule() { add(Utils.helpFormat("%c $WOLFRAM_CMD days until christmas")) add(Utils.helpFormat("%c $WOLFRAM_CMD distance earth moon units=metric")) } - initProperties(APPID_KEY_PROP, UNITS_PROP) + initProperties(WOLFRAM_APPID_KEY, WOLFRAM_UNITS_PROP) } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ChatGptTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ChatGptTest.kt index 6c30297..98f0052 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ChatGptTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ChatGptTest.kt @@ -34,8 +34,10 @@ package net.thauvin.erik.mobibot.modules import assertk.assertThat import assertk.assertions.contains import assertk.assertions.hasNoCause +import assertk.assertions.isEqualTo import assertk.assertions.isFailure import assertk.assertions.isInstanceOf +import assertk.assertions.message import net.thauvin.erik.mobibot.LocalProperties import org.testng.annotations.Test @@ -50,7 +52,7 @@ class ChatGptTest : LocalProperties() { @Test(groups = ["modules", "no-ci"]) fun testChat() { - val apiKey = getProperty(ChatGpt.API_KEY_PROP) + val apiKey = getProperty(ChatGpt.CHATGPT_API_KEY) assertThat( ChatGpt.chat("how do I make an HTTP request in Javascript?", apiKey, 100) ).contains("XMLHttpRequest") diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt index 9d052cf..8d12ab4 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt @@ -75,8 +75,8 @@ class GoogleSearchTest : LocalProperties() { @Test(groups = ["no-ci", "modules"]) @Throws(ModuleException::class) fun testSearchGoogle() { - val apiKey = getProperty(GoogleSearch.API_KEY_PROP) - val cseKey = getProperty(GoogleSearch.CSE_KEY_PROP) + val apiKey = getProperty(GoogleSearch.GOOGLE_API_KEY_PROP) + val cseKey = getProperty(GoogleSearch.GOOGLE_CSE_KEY_PROP) try { var query = "mobibot" diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt index 2721469..162550f 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt @@ -59,7 +59,7 @@ class StockQuoteTest : LocalProperties() { @Test(groups = ["modules"]) @Throws(ModuleException::class) fun testGetQuote() { - val apiKey = getProperty(StockQuote.API_KEY_PROP) + val apiKey = getProperty(StockQuote.ALPHAVANTAGE_API_KEY_PROP) try { var symbol = "apple inc" val messages = getQuote(symbol, apiKey) diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/Weather2Test.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/Weather2Test.kt index f1949d6..6c67536 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/Weather2Test.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/Weather2Test.kt @@ -46,7 +46,7 @@ import assertk.assertions.prop import net.aksingh.owmjapis.api.APIException import net.aksingh.owmjapis.core.OWM import net.thauvin.erik.mobibot.LocalProperties -import net.thauvin.erik.mobibot.modules.Weather2.Companion.API_KEY_PROP +import net.thauvin.erik.mobibot.modules.Weather2.Companion.OWM_API_KEY_PROP import net.thauvin.erik.mobibot.modules.Weather2.Companion.ftoC import net.thauvin.erik.mobibot.modules.Weather2.Companion.getCountry import net.thauvin.erik.mobibot.modules.Weather2.Companion.getWeather @@ -86,7 +86,7 @@ class Weather2Test : LocalProperties() { @Throws(ModuleException::class) fun testWeather() { var query = "98204" - var messages = getWeather(query, getProperty(API_KEY_PROP)) + var messages = getWeather(query, getProperty(OWM_API_KEY_PROP)) assertThat(messages, "getWeather($query)").index(0).prop(Message::msg).all { contains("Everett, United States") contains("US") @@ -94,7 +94,7 @@ class Weather2Test : LocalProperties() { assertThat(messages, "getWeather($query)").index(messages.size - 1).prop(Message::msg).endsWith("98204%2CUS") query = "San Francisco" - messages = getWeather(query, getProperty(API_KEY_PROP)) + messages = getWeather(query, getProperty(OWM_API_KEY_PROP)) assertThat(messages, "getWeather($query)").index(0).prop(Message::msg).all { contains("San Francisco") contains("US") @@ -102,7 +102,7 @@ class Weather2Test : LocalProperties() { assertThat(messages, "getWeather($query)").index(messages.size - 1).prop(Message::msg).endsWith("5391959") query = "London, GB" - messages = getWeather(query, getProperty(API_KEY_PROP)) + messages = getWeather(query, getProperty(OWM_API_KEY_PROP)) assertThat(messages, "getWeather($query)").index(0).prop(Message::msg).all { contains("London, United Kingdom") contains("GB") @@ -111,7 +111,7 @@ class Weather2Test : LocalProperties() { try { query = "Foo, US" - getWeather(query, getProperty(API_KEY_PROP)) + getWeather(query, getProperty(OWM_API_KEY_PROP)) } catch (e: ModuleException) { assertThat(e.cause, "getWeather($query)").isNotNull().isInstanceOf(APIException::class.java) } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/WolframAlphaTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/WolframAlphaTest.kt index 0d9ed9a..f0fb424 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/WolframAlphaTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/WolframAlphaTest.kt @@ -58,7 +58,7 @@ class WolframAlphaTest : LocalProperties() { @Test(groups = ["modules", "no-ci"]) @Throws(ModuleException::class) fun queryWolframTest() { - val apiKey = getProperty(WolframAlpha.APPID_KEY_PROP) + val apiKey = getProperty(WolframAlpha.WOLFRAM_APPID_KEY) try { var query = "SFO to SEA" assertThat(queryWolfram(query, appId = apiKey), "queryWolfram($query)").contains("miles") diff --git a/version.properties b/version.properties index 8d6a471..c049177 100644 --- a/version.properties +++ b/version.properties @@ -1,9 +1,9 @@ #Generated by the Semver Plugin for Gradle -#Thu Jan 12 23:03:48 PST 2023 -version.buildmeta=982 +#Thu Jan 12 00:57:56 PST 2023 +version.buildmeta=975 version.major=0 version.minor=8 version.patch=0 version.prerelease=rc version.project=mobibot -version.semver=0.8.0-rc+982 +version.semver=0.8.0-rc+975