浏览代码

Implementing art and fixing the imagine command to use a real API key

Kirk Trombley 5 年之前
父节点
当前提交
c2355a4046
共有 2 个文件被更改,包括 31 次插入5 次删除
  1. 3 0
      config/secrets.toml.template
  2. 28 5
      src/plugins/querying.py

+ 3 - 0
config/secrets.toml.template

@@ -28,3 +28,6 @@ port = 32400
 
 [democracy]
 candidates = ["A", "B"]
+
+[text2img]
+api_key = "KEY"

+ 28 - 5
src/plugins/querying.py

@@ -15,7 +15,7 @@ from .util import upload_image
 def inspire(message):
     argstr = message.raw_args
 
-    if argstr is not None and "explain" in argstr:
+    if argstr is not None and "!explain" in argstr:
         return "Using the command !inspire will request an inspiration image from http://inspirobot.me/"
 
     try:
@@ -221,6 +221,8 @@ def selfie(message):
 
 @as_plugin
 def cat(message):
+    if message.raw_args is not None and "!explain" in message.raw_args:
+        return "The !cat grabs a cat from https://thiscatdoesnotexist.com/"
     try:
         r = requests.get("https://thiscatdoesnotexist.com/", headers={ "User-Agent": "Rollbot" })
     except ConnectionError as e:
@@ -230,13 +232,17 @@ def cat(message):
     
 @as_plugin
 def imagine(message):
+    if message.raw_args is None:
+        return RollbotFailure.INVALID_ARGUMENTS.with_reason("The !imagine command needs text to imagine!")
+
+    if "!explain" in message.raw_args:
+        return "The !imagine command uses the text2img API at https://deepai.org/machine-learning-model/text2img"
+
     try:
         r = requests.post(
             "https://api.deepai.org/api/text2img",
             data={ "text": message.raw_args },
-            # TODO rules for this API key seem unclear on the site
-            # if this stops working, maybe refresh the key
-            headers={ "api-key": "quickstart-QUdJIGlzIGNvbWluZy4uLi4K"}
+            headers={ "api-key": get_secret("text2img.api_key")}
         )
     except ConnectionError as e:
         return RollbotFailure.SERVICE_DOWN.with_reason("Could not reach text2img service.").with_exception(e)
@@ -245,4 +251,21 @@ def imagine(message):
     if result is None:
         return RollbotFailure.SERVICE_DOWN.with_reason(f"Response from text2img was invalid: {r.text}")
     
-    return RollbotResponse(message, img=result)
+    return RollbotResponse(message, img=result)
+
+art_number = 0
+
+@as_plugin
+def art(message):
+    if message.raw_args is not None and "!explain" in message.raw_args:
+        return "The !art command uses the 9gans gallery at https://9gans.com/ which generates 9 images every hour."
+
+    global art_number
+    art_number += 1
+    art_number %= 9
+    try:
+        r = requests.get(f"https://storage.googleapis.com/9gans/mini/{art_number + 1}.jpg")
+    except ConnectionError as e:
+        return RollbotFailure.SERVICE_DOWN.with_reason("Could not reach art generator.").with_exception(e)
+
+    return run_upload(message, r.content)