python irc bot 试图使用两个不同的消息系统
python irc bot trying to consume two different messaging system
我正在使用 python irc 模块 [1] 和 python 中的 pika 模块来创建一个 irc 机器人,它可以侦听频道消息和 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 <joel@rosdahl.net>
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_consuming()' 行,并且
显然,如果我将 channel.start_consuming() 移动到 reactor.process_forever() 上方,
reactor.process_forever() 没有 运行.
在这一点上,我被难住了。我考虑过使用多处理线程;但是我的
线程方面的经验是零,即使在阅读 [3] 之后,我也不完全确定
那会有所帮助。老实说,这让我更加困惑。
我想添加一个 on_* 回调处理程序,但由于这些事件是
全部基于 irc,处理程序不会监听 rabbitmq 队列。
有人可以建议如何同时 运行 和 process_forever()
循环和 start_consuming() 循环;也就是说,让机器人
收听 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 <joel@rosdahl.net>
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,感谢您的帮助和建议!
:埃德
我正在使用 python irc 模块 [1] 和 python 中的 pika 模块来创建一个 irc 机器人,它可以侦听频道消息和 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 <joel@rosdahl.net>
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_consuming()' 行,并且 显然,如果我将 channel.start_consuming() 移动到 reactor.process_forever() 上方, reactor.process_forever() 没有 运行.
在这一点上,我被难住了。我考虑过使用多处理线程;但是我的 线程方面的经验是零,即使在阅读 [3] 之后,我也不完全确定 那会有所帮助。老实说,这让我更加困惑。
我想添加一个 on_* 回调处理程序,但由于这些事件是 全部基于 irc,处理程序不会监听 rabbitmq 队列。
有人可以建议如何同时 运行 和 process_forever() 循环和 start_consuming() 循环;也就是说,让机器人 收听 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 <joel@rosdahl.net>
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,感谢您的帮助和建议!
:埃德