meeting_bot.py 2.5 KB

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