groupme_bot.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import atexit
  2. from logging.config import dictConfig
  3. from threading import Thread
  4. import random
  5. from flask import Flask, request
  6. import requests
  7. from rollbot import Rollbot, RollbotMessage, RollbotPlugin
  8. import plugins
  9. import db
  10. from config import BOTS_LOOKUP, get_config, get_secret
  11. GLOBAL_ADMINS = get_secret("auths.global")
  12. GROUP_ADMINS = get_secret("auths.group")
  13. dictConfig({
  14. "version": 1,
  15. "formatters": {"default": {
  16. "format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s",
  17. }},
  18. "handlers": {"wsgi": {
  19. "class": "logging.StreamHandler",
  20. "stream": "ext://flask.logging.wsgi_errors_stream",
  21. "formatter": "default"
  22. }},
  23. "root": {
  24. "level": "INFO",
  25. "handlers": ["wsgi"]
  26. }
  27. })
  28. def post_groupme_message(msg, group_id):
  29. bot_id = BOTS_LOOKUP[group_id]
  30. requests.post(
  31. "https://api.groupme.com/v3/bots/post",
  32. json={
  33. "bot_id": bot_id,
  34. "text": msg
  35. },
  36. timeout=10
  37. )
  38. sponsor_count = -1
  39. sponsors = [
  40. ("RAID: Shadow Legends - the hot new mobile game with over a billion players", "RAIDBOT"),
  41. ("SquareSpace - it's like if VistaPrint was more expensive", "WEBBOT"),
  42. ("Raycon Earbuds - the best true wireless earbuds that sponsor Rollbot", "ROLLBUDS"),
  43. ("DoorDash - we maybe stopped stealing tips", "DOORBOT"),
  44. ("Totinos - I love their products", "TOTINOS"),
  45. ("Stamps dot com - do people still send letters?", "STAMPBOT"),
  46. ("Bowl and Branch - not the people that make linens, we just sell bowls and branches", "BOLLBOT"),
  47. ("Audible dot com - sign up today for a free license to view an eBook", "READBOT"),
  48. ("Casper Mattresses - the beds are haunted, actually haunted! How is that allowed?", "ROLLBED"),
  49. ("MeUndies - think about rollbot but on your junk", "ROLLBOXERS I guess"),
  50. ("PepsiCo Products - the refreshing taste of PepsiCo products", "Sinaltrainal v. Coca-Cola Co."),
  51. ("RAIDCON Shadow Buds - nightmare nightmare nightmare nightmare", "nightmarenightmarenightmare"),
  52. ]
  53. def post_sponsored(msg, group_id):
  54. global sponsor_count
  55. if random.randint(1, 3) == 3:
  56. sponsor_count += 1
  57. (sponsor, code) = sponsors[sponsor_count] if sponsor_count < len(sponsors) else random.choice(sponsors)
  58. post_groupme_message(f"This action was sponsored by {sponsor}! Use code {code} for {random.choice([10, 15, 25])}% off!", group_id)
  59. post_groupme_message(msg, group_id)
  60. app = Flask(__name__)
  61. app.config["PROPAGATE_EXCEPTIONS"] = True
  62. rollbot = Rollbot(
  63. logger=app.logger,
  64. plugin_classes=RollbotPlugin.find_all_plugins(),
  65. aliases=get_config("aliases"),
  66. responses=get_config("responses"),
  67. sleep_time=float(get_config("sleep_time")),
  68. callback=post_sponsored,
  69. session_factory=db.Session
  70. )
  71. app.logger.info("Initializing database tables")
  72. db.init_db()
  73. app.logger.info("Finished initializing database")
  74. rollbot.start_plugins()
  75. atexit.register(rollbot.shutdown_plugins)
  76. @app.route("/", methods=["POST"])
  77. def execute():
  78. json = request.get_json()
  79. msg = RollbotMessage.from_groupme(json, global_admins=GLOBAL_ADMINS, group_admins=GROUP_ADMINS)
  80. if msg.group_id not in BOTS_LOOKUP:
  81. app.logger.warning(f"Received message from unknown group ID {msg.group_id}")
  82. return "", 400
  83. t = Thread(target=lambda: rollbot.handle_command(msg))
  84. t.start()
  85. return "", 204
  86. @app.route("/health")
  87. def health():
  88. return "Rollbot healthy!", 200
  89. if __name__ == "__main__":
  90. app.run(host="0.0.0.0", port=6070)