groupme_driver.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. from __future__ import annotations
  2. from datetime import datetime
  3. import json
  4. import traceback
  5. import asyncio
  6. import logging
  7. import os
  8. import time
  9. import toml
  10. from fastapi import FastAPI, BackgroundTasks, Response
  11. from pydantic import BaseModel
  12. import rollbot
  13. from commands import config
  14. logging.config.fileConfig("logging.conf", disable_existing_loggers=False)
  15. with open(os.environ.get("SECRET_FILE", "secrets.toml"), "r") as sfile:
  16. secrets = toml.load(sfile)
  17. database_file = os.environ.get("DATABASE_FILE", secrets["database_file"])
  18. groupme_bots = secrets["groupme"]["bots"]
  19. groupme_token = secrets["groupme"]["token"]
  20. replies_enabled = secrets["groupme"].get("replies_enabled", True)
  21. groupme_admins = secrets["admins"]["origin"]
  22. group_admins = secrets["admins"]["channel"]
  23. imgur_fallback = secrets["groupme"].get("imgur_fallback", False)
  24. imgur_token = secrets["imgur"]["token"] if imgur_fallback else None
  25. max_msg_len = 1000
  26. split_text = "\n..."
  27. msg_slice = max_msg_len - len(split_text)
  28. class GroupMeMessage(BaseModel):
  29. id: str
  30. sender_id: str
  31. group_id: str
  32. name: str
  33. text: str
  34. created_at: int
  35. attachments: list[dict[str, str]]
  36. class GroupMeBot(rollbot.Rollbot[GroupMeMessage]):
  37. def __init__(self):
  38. super().__init__(config.extend(rollbot.CommandConfiguration(bangs=("!","/",))), database_file)
  39. def read_config(self, key: str) -> Any:
  40. cfg = secrets
  41. for part in key.split("."):
  42. cfg = cfg.get(part, None)
  43. if cfg is None:
  44. return None
  45. return cfg
  46. def _convert_attachment(self, group_id, att_type, att_body):
  47. if att_type == "reply":
  48. async def fetch_reply():
  49. try:
  50. async with self.context.request.get(
  51. f"https://api.groupme.com/v3/groups/{group_id}/messages",
  52. headers={
  53. "Content-Type": "application/json",
  54. },
  55. params={
  56. "token": groupme_token,
  57. "limit": 1,
  58. "after_id": str(int(att_body["reply_id"]) - 1),
  59. },
  60. ) as resp:
  61. msg = (await resp.json())["response"]["messages"][0]
  62. return json.dumps(msg)
  63. except:
  64. self.context.logger.exception("Failed to look up attached message")
  65. return rollbot.Attachment(att_type, fetch_reply)
  66. return rollbot.Attachment(att_type, json.dumps(att_body))
  67. async def parse(self, msg: GroupMeMessage):
  68. return rollbot.Message(
  69. origin_id="GROUPME",
  70. channel_id=msg.group_id,
  71. sender_id=msg.sender_id,
  72. timestamp=datetime.fromtimestamp(msg.created_at),
  73. origin_admin=msg.sender_id in groupme_admins,
  74. channel_admin=msg.sender_id in group_admins.get(msg.group_id, ()),
  75. sender_name=msg.name,
  76. text=msg.text,
  77. attachments=[self._convert_attachment(msg.group_id, d["type"], d) for d in msg.attachments],
  78. message_id=msg.id,
  79. )
  80. async def on_message(self, incoming: GroupMeMessage):
  81. parsed = await self.parse(incoming)
  82. if parsed.sender_name.lower() == "rollbot":
  83. return
  84. await self.respond(rollbot.Response.from_message(
  85. parsed,
  86. f"Hey {parsed.sender_name}, GroupMe is a dying chat platform, please consider using Discord instead!",
  87. [rollbot.Attachment("reply", parsed.message_id)]
  88. ))
  89. async def upload_image(self, data: bytes) -> tuple[str, str]:
  90. try:
  91. return await self.upload_image_groupme(data), ""
  92. except:
  93. self.context.logger.exception("GroupMe image upload failure")
  94. if not imgur_fallback:
  95. self.context.logger.info("Imgur fallback disabled")
  96. raise
  97. self.context.logger.info("Attempting imgur fallback")
  98. return await self.upload_image_imgur(data), "GroupMe rejected image upload, falling back to Imgur...\n"
  99. async def upload_image_groupme(self, data: bytes) -> str:
  100. async with self.context.request.post(
  101. "https://image.groupme.com/pictures",
  102. headers={
  103. "Content-Type": "image/png",
  104. "X-Access-Token": groupme_token,
  105. },
  106. data=data,
  107. ) as upload:
  108. upload.raise_for_status()
  109. return (await upload.json())["payload"]["url"]
  110. async def upload_image_imgur(self, data: bytes) -> str:
  111. async with self.context.request.post(
  112. "https://api.imgur.com/3/image",
  113. headers={
  114. "Authorization": f"Client-ID {imgur_token}",
  115. },
  116. data={"image": data},
  117. ) as upload:
  118. upload.raise_for_status()
  119. return (await upload.json())["data"]["link"]
  120. async def post_message(self, bot_id: str, text: str, attachments: list[dict[str, str]]):
  121. body = {
  122. "bot_id": bot_id,
  123. "text": text,
  124. "attachments": attachments,
  125. }
  126. self.context.logger.info(f"Sending: {body}")
  127. result = await self.context.request.post(
  128. "https://api.groupme.com/v3/bots/post",
  129. json=body,
  130. timeout=10,
  131. )
  132. self.context.logger.info(f"Received: {result.status} - {await result.text()}")
  133. async def respond(self, res: rollbot.Response):
  134. if res.cause is not None and (proc_time := time.time() - res.cause.received_at) < 1:
  135. # sleep for a moment to make groupme not misorder messages
  136. await asyncio.sleep(1 - proc_time)
  137. if res.origin_id != "GROUPME":
  138. self.context.logger.error(f"Unable to respond to {res.origin_id}")
  139. return
  140. bot_id = groupme_bots.get(res.channel_id, None)
  141. if bot_id is None:
  142. self.context.logger.error(f"Unable to respond to group {res.channel_id} in GroupMe")
  143. return
  144. message = ""
  145. attachments = []
  146. try:
  147. if res.attachments is not None:
  148. for att in res.attachments:
  149. if att.name == "image":
  150. if isinstance(att.body, bytes):
  151. url, note = await self.upload_image(att.body)
  152. attachments.append(
  153. {
  154. "type": "image",
  155. "url": url,
  156. }
  157. )
  158. message += note
  159. else:
  160. attachments.append({"type": "image", "url": att.body})
  161. if replies_enabled and att.name == "reply":
  162. if att.body is None or not isinstance(att.body, str):
  163. raise ValueError("Invalid reply body type, must be message ID")
  164. attachments.append({
  165. "type": "reply",
  166. "base_reply_id": att.body,
  167. "reply_id": att.body,
  168. })
  169. except:
  170. self.context.debugging = "".join(traceback.format_exc())
  171. message += "Failed to upload one or more attachments!\n"
  172. self.context.logger.exception("Failed to upload attachment")
  173. if res.text is not None:
  174. message += res.text
  175. msgs = []
  176. while len(message) > max_msg_len:
  177. msgs.append(message[:msg_slice] + split_text)
  178. message = message[msg_slice:]
  179. msgs.append(message)
  180. await self.post_message(bot_id, msgs[0], attachments)
  181. for m in msgs[1:]:
  182. await asyncio.sleep(0.1)
  183. await self.post_message(bot_id, m, [])
  184. app = FastAPI()
  185. bot = GroupMeBot()
  186. @app.on_event("startup")
  187. async def startup():
  188. await bot.on_startup()
  189. @app.on_event("shutdown")
  190. async def shutdown():
  191. await bot.on_shutdown()
  192. @app.post("/", status_code=204)
  193. async def receive(message: GroupMeMessage, tasks: BackgroundTasks):
  194. tasks.add_task(bot.on_message, message)
  195. return Response(status_code=204)
  196. @app.get("/health", status_code=204)
  197. def health():
  198. return Response(status_code=204)