demo_priv_meeting_bot.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # TODO
  2. # 1) Enable for multiple chats
  3. import json
  4. import asyncio
  5. async def start():
  6. with open('meeting_bot_config.json', 'r') as config:
  7. data = json.load(config)
  8. host = data['host']
  9. port = data['port']
  10. channel = data['channel']
  11. nickname = data['nickname']
  12. secret = data['secret']
  13. print(f"Start a connection {host}:{port}")
  14. reader, writer = await asyncio.open_connection(host, port)
  15. print("Send CAP msg")
  16. cap_msg = f"CAP REQ : no-history \r\n"
  17. writer.write(cap_msg.encode('utf8'))
  18. print("Send NICK msg")
  19. nick_msg = f"NICK {nickname} \r\n"
  20. writer.write(nick_msg.encode('utf8'))
  21. print("Send CAP END msg")
  22. cap_end_msg = f"CAP END \r\n"
  23. writer.write(cap_end_msg.encode('utf8'))
  24. print(f"Send JOIN msg: {channel}")
  25. join_msg = f"JOIN {channel} \r\n"
  26. writer.write(join_msg.encode('utf8'))
  27. topics = []
  28. print("Start...")
  29. while True:
  30. msg = await reader.read(1024)
  31. msg = msg.decode('utf8').strip()
  32. if not msg:
  33. continue
  34. command = msg.split(" ")[1]
  35. if command == "PRIVMSG":
  36. msg_title = msg.split(" ")[3][1:]
  37. if not msg_title:
  38. continue
  39. reply = None
  40. if msg_title == "!start":
  41. reply = f"PRIVMSG {channel} :meeting started \r\n"
  42. msg_title = "!list"
  43. if msg_title == "!end":
  44. reply = f"PRIVMSG {channel} :meeting end \r\n"
  45. topics = []
  46. if msg_title == "!topic":
  47. topic = msg.split(" ", 4)
  48. if len(topic) != 5:
  49. continue
  50. topic = topic[4]
  51. topics.append(topic)
  52. reply = f"PRIVMSG {channel} :add topic: {topic} \r\n"
  53. if msg_title == "!list":
  54. rep = f"PRIVMSG {channel} :topics: \r\n"
  55. writer.write(rep.encode('utf8'))
  56. for i, topic in enumerate(topics, 1):
  57. rep = f"PRIVMSG {channel} :{i}-{topic} \r\n"
  58. writer.write(rep.encode('utf8'))
  59. await writer.drain()
  60. if msg_title == "!next":
  61. if len(topics) == 0:
  62. reply = f"PRIVMSG {channel} :no topics \r\n"
  63. else:
  64. tp = topics.pop(0)
  65. reply = f"PRIVMSG {channel} :current topic: {tp} \r\n"
  66. if reply != None:
  67. writer.write(reply.encode('utf8'))
  68. await writer.drain()
  69. if command == "QUIT":
  70. break
  71. writer.close()
  72. asyncio.run(start())