meeting_bot.py 2.4 KB

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