meeting_bot.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. print("Error: Receive empty msg")
  28. break
  29. command = msg.split(" ")[1]
  30. if command == "PRIVMSG":
  31. msg_title = msg.split(" ")[3][1:]
  32. if not msg_title:
  33. continue
  34. reply = None
  35. if msg_title == "#m_start":
  36. reply = f"PRIVMSG {channel} :meeting started \r\n"
  37. msg_title = "#m_list"
  38. if msg_title == "#m_end":
  39. reply = f"PRIVMSG {channel} :meeting end \r\n"
  40. topics = []
  41. if msg_title == "#m_topic":
  42. topic = msg.split(" ", 4)
  43. if len(topic) != 5:
  44. continue
  45. topic = topic[4]
  46. topics.append(topic)
  47. reply = f"PRIVMSG {channel} :add topic: {topic} \r\n"
  48. if msg_title == "#m_list":
  49. rep = f"PRIVMSG {channel} :topics: \r\n"
  50. writer.write(rep.encode('utf8'))
  51. for i, topic in enumerate(topics, 1):
  52. rep = f"PRIVMSG {channel} :{i}-{topic} \r\n"
  53. writer.write(rep.encode('utf8'))
  54. await writer.drain()
  55. if msg_title == "#m_next":
  56. if len(topics) == 0:
  57. reply = f"PRIVMSG {channel} :no topics \r\n"
  58. else:
  59. tp = topics.pop(0)
  60. reply = f"PRIVMSG {channel} :current topic: {tp} \r\n"
  61. if reply != None:
  62. writer.write(reply.encode('utf8'))
  63. await writer.drain()
  64. if command == "QUIT":
  65. break
  66. writer.close()
  67. asyncio.run(start())