groupme_bot.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. ("Raycon Earbuds - the best true wireless earbuds that sponsor Rollbot", "ROLLBUDS"),
  42. ("Audible dot com - sign up today for a free license to view an eBook", "READBOT"),
  43. ("Casper Mattresses - the beds are haunted, actually haunted! How is that allowed?", "ROLLBED"),
  44. ("MeUndies - think about rollbot but on your junk", "ROLLBOXERS I guess"),
  45. ("PepsiCo Products - the refreshing taste of PepsiCo products", "Sinaltrainal v. Coca-Cola Co."),
  46. ("RAIDCON Shadow Buds - nightmare nightmare nightmare nightmare", "nightmarenightmarenightmare"),
  47. ]
  48. def post_sponsored(msg, group_id):
  49. global sponsor_count
  50. if random.randint(1, 3) == 3:
  51. sponsor_count += 1
  52. (sponsor, code) = sponsors[sponsor_count] if sponsor_count < len(sponsors) else random.choice(sponsors)
  53. post_groupme_message(f"This action was sponsored by {sponsor}! Use code {code} for {random.choice([10, 15, 25])}% off!", group_id)
  54. post_groupme_message(msg, group_id)
  55. app = Flask(__name__)
  56. app.config["PROPAGATE_EXCEPTIONS"] = True
  57. rollbot = Rollbot(
  58. logger=app.logger,
  59. plugin_classes=RollbotPlugin.find_all_plugins(),
  60. aliases=get_config("aliases"),
  61. responses=get_config("responses"),
  62. sleep_time=float(get_config("sleep_time")),
  63. callback=post_sponsored,
  64. session_factory=db.Session
  65. )
  66. app.logger.info("Initializing database tables")
  67. db.init_db()
  68. app.logger.info("Finished initializing database")
  69. rollbot.start_plugins()
  70. atexit.register(rollbot.shutdown_plugins)
  71. @app.route("/", methods=["POST"])
  72. def execute():
  73. json = request.get_json()
  74. msg = RollbotMessage.from_groupme(json, global_admins=GLOBAL_ADMINS, group_admins=GROUP_ADMINS)
  75. if msg.group_id not in BOTS_LOOKUP:
  76. app.logger.warning(f"Received message from unknown group ID {msg.group_id}")
  77. return "", 400
  78. t = Thread(target=lambda: rollbot.handle_command(msg))
  79. t.start()
  80. return "", 204
  81. @app.route("/health")
  82. def health():
  83. return "Rollbot healthy!", 200
  84. if __name__ == "__main__":
  85. app.run(host="0.0.0.0", port=6070)