taubot.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import argparse
  2. import irc
  3. import json
  4. import socket
  5. import sys
  6. # parse arguments
  7. parser = argparse.ArgumentParser(description='IRC bot to send a pipe to an IRC channel')
  8. parser.add_argument('--server',default='127.0.0.1', help='IRC server')
  9. parser.add_argument('--port', default=11066, help='port of the IRC server')
  10. parser.add_argument('--nickname', help='bot nickname in IRC')
  11. parser.add_argument('--channel', default="#dev", action='append', help='channel to join')
  12. parser.add_argument('--pipe', default="/tmp/tau_pipe" , help='pipe to read from')
  13. parser.add_argument('--skip', default="prv", help='Project or Tags to skip notifications for')
  14. parser.add_argument('--alt-chan', default="#test", required='--skip' in sys.argv, help='Alternative channel to send notifications to when there are skipped tasks')
  15. args = parser.parse_args()
  16. channels = [args.channel, args.alt_chan] if args.alt_chan is not None else args.channel
  17. ircc = irc.IRC()
  18. ircc.connect(args.server,int(args.port), channels, args.nickname)
  19. while True:
  20. with open(args.pipe) as handle:
  21. while True:
  22. log_line = handle.readline()
  23. if not log_line:
  24. break
  25. print(log_line)
  26. print("======================================")
  27. task = json.loads(log_line)
  28. channel = args.channel
  29. for event in task['events']:
  30. cmd = event['action']
  31. if cmd == "add_task":
  32. user = task['owner']
  33. id = task['id']
  34. title = task['title']
  35. assigned = ", ".join(task['assign'])
  36. project = task['project'] if task['project'] is not None else []
  37. if args.skip in project or args.skip in task['tags']:
  38. channel = args.alt_chan
  39. if len(assigned) > 0:
  40. notification = f"{user} added task ({id}): {title}. assigned to {assigned}"
  41. else:
  42. notification = f"{user} added task ({id}): {title}"
  43. # print(notification)
  44. ircc.send(channel, notification)
  45. elif cmd == "state":
  46. user = event['author']
  47. state = event['content']
  48. id = task['id']
  49. title = task['title']
  50. project = task['project'] if task['project'] is not None else []
  51. if args.skip in project or args.skip in task['tags']:
  52. channel = args.alt_chan
  53. if state == "start":
  54. notification = f"{user} started task ({id}): {title}"
  55. elif state == "pause":
  56. notification = f"{user} paused task ({id}): {title}"
  57. elif state == "stop":
  58. notification = f"{user} stopped task ({id}): {title}"
  59. elif state == "cancel":
  60. notification = f"{user} canceled task ({id}): {title}"
  61. # print(notification)
  62. ircc.send(channel, notification)
  63. elif cmd == "comment":
  64. user = event['author']
  65. id = task['id']
  66. title = task['title']
  67. project = task['project'] if task['project'] is not None else []
  68. if args.skip in project or args.skip in task['tags']:
  69. channel = args.alt_chan
  70. notification = f"{user} commented on task ({id}): {title}"
  71. # print(notification)
  72. ircc.send(channel, notification)
  73. elif cmd == "assign":
  74. user = event['author']
  75. assignees = event['content']
  76. id = task['id']
  77. title = task['title']
  78. project = task['project'] if task['project'] is not None else []
  79. if args.skip in project or args.skip in task['tags']:
  80. channel = args.alt_chan
  81. notification = f"{user} reassigned task ({id}): {title} to {assignees}"
  82. # print(notification)
  83. ircc.send(channel, notification)