meeting_bot.py 2.1 KB

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