[我正在使用python irc模块[1]和python中的pika模块来创建一个irc机器人,该bot侦听通道消息和Rabbitmq队列。
我从[2]中获取源代码,并在其中添加了pika元素:
#! /usr/bin/env python
#
# Example program using irc.client.
#
# This program is free without restrictions; do anything you like with
# it.
#
# Joel Rosdahl <[email protected]>
import sys
import argparse
import itertools
import irc.client
import pika
target = "#test"
"The nick or channel to which to send messages"
def on_connect(connection, event):
if irc.client.is_channel(target):
connection.join(target)
return
main_loop(connection)
def on_join(connection, event):
main_loop(connection)
def get_lines():
while True:
yield sys.stdin.readline().strip()
def main_loop(connection):
for line in itertools.takewhile(bool, get_lines()):
print(line)
connection.privmsg(target, line)
connection.quit("Using irc.client.py")
def on_disconnect(connection, event):
raise SystemExit()
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('server')
parser.add_argument('nickname')
parser.add_argument('target', help="a nickname or channel")
parser.add_argument('-p', '--port', default=6667, type=int)
jaraco.logging.add_arguments(parser)
return parser.parse_args()
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
def get_channel():
creds = pika.PlainCredentials('testuser', 'testing')
params = pika.ConnectionParameters(
host="localhost",
virtual_host="/test",
credentials=creds)
connection = pika.BlockingConnection(params)
channel = connection.channel()
channel.queue_declare(queue='test')
channel.basic_consume(
queue='test', on_message_callback=callback, auto_ack=True)
return channel
def main():
chan = get_channel()
reactor = irc.client.Reactor()
try:
c = reactor.server().connect("irc.local", 6667, "testuser")
except irc.client.ServerConnectionError:
print(sys.exc_info()[1])
raise SystemExit(1)
c.add_global_handler("welcome", on_connect)
c.add_global_handler("join", on_join)
c.add_global_handler("disconnect", on_disconnect)
print("Processing reactor")
reactor.process_forever()
print("Channel : start consuming")
channel.start_consuming()
if __name__ == '__main__':
main()
上述代码的问题是,我没有将get_lines()代码实际修改为从消息队列中获取消息,因为我一直坚持要更改的内容。
此外,'reactor.process_forever()'行会阻塞'channel.start_consumping()'行,显然,如果我将channel.start_using()移到Reactor.process_forever()之上,反应器.process_forever()无法运行。
至此,我很困惑。我考虑过使用多处理线程。但是我的使用线程的经验为零,即使阅读[3],我也不确定这会有所帮助。老实说,这让我更加困惑。
我曾考虑添加on_ *回调处理程序,但由于这些事件是所有基于irc的处理程序都不会监听Rabbitmq队列。
可能有人会对如何同时运行process_forever()提出建议循环和start_consumption()循环;也就是说,让机器人收听irc频道和消息传递队列?
谢谢!
:ed
[1]-https://github.com/jaraco/irc
[2]-https://github.com/jaraco/irc/blob/master/scripts/irccat.py
感谢@fura(非常感谢!)帮助您澄清我的工作。最终的工作结果代码如下:
#! /usr/bin/env python
#
# Example program using irc.client.
#
# This program is free without restrictions; do anything you like with
# it.
#
# Joel Rosdahl <[email protected]>
import sys
import argparse
import itertools
import irc.client
import pika
target = "#test"
"The nick or channel to which to send messages"
def on_connect(connection, event):
if irc.client.is_channel(target):
connection.join(target)
return
def on_disconnect(connection, event):
raise SystemExit()
def get_channel():
creds = pika.PlainCredentials('testuser', 'testing')
params = pika.ConnectionParameters(
host="msg.local",
virtual_host="/test",
credentials=creds)
connection = pika.BlockingConnection(params)
channel = connection.channel()
channel.queue_declare(queue='test')
return channel
def main():
chan = get_channel()
reactor = irc.client.Reactor()
try:
print("connect to server")
c = reactor.server().connect("irc.local", 6667, "testUser")
except irc.client.ServerConnectionError:
print(sys.exc_info()[1])
raise SystemExit(1)
c.add_global_handler("welcome", on_connect)
c.add_global_handler("disconnect", on_disconnect)
print("Processing reactor")
while True:
reactor.process_once()
mf, hdr, bd = chan.basic_get("test")
if mf:
chan.basic_ack(mf.delivery_tag)
bdd = bd.decode('utf-8')
if "cmd:" in bdd:
p = bdd.replace("cmd:", "").strip()
if p.lower() == "quit":
c.quit("Buckeroo Banzai!")
else:
c.privmsg("#test", bdd)
else:
c.privmsg("#test", bdd)
if __name__ == '__main__':
main()
当然,我用短消息对此进行了测试。.还没有通过它发送巨大的文本文件,因此不确定它是否有效或浪费资源。需要将其付诸实践以查看是否窒息。
再次,@ fura,感谢您的帮助和建议!
:Ed