我需要发送简单的文本消息,等待答案并将此联系人的消息重新发送到另一个号码。下面的代码正在发送第一条消息,但是我不知道如何正确启动echo client。我做错了什么? 最好的问候。
from yowsup.layers.interface import YowInterfaceLayer, ProtocolEntityCallback
from yowsup.layers.protocol_messages.protocolentities import TextMessageProtocolEntity
from yowsup.common.tools import Jid
import threading
import logging
logger = logging.getLogger(__name__)
from yowsup.stacks import YowStackBuilder
from yowsup.layers.auth import AuthError
from yowsup.layers import YowLayerEvent
from yowsup.layers.auth import YowAuthenticationProtocolLayer
from yowsup.layers.network import YowNetworkLayer
from yowsup.layers import YowLayer
from yowsup.env import YowsupEnv
发送图层创建
class SendLayer(YowInterfaceLayer):
#This message is going to be replaced by the @param message in YowsupSendStack construction
#i.e. list of (jid, message) tuples
PROP_MESSAGES = "org.openwhatsapp.yowsup.prop.sendclient.queue"
def __init__(self):
super(SendLayer, self).__init__()
self.ackQueue = []
self.lock = threading.Condition()
#call back function when there is a successful connection to whatsapp server
@ProtocolEntityCallback("success")
def onSuccess(self, successProtocolEntity):
self.lock.acquire()
#for target in self.getProp(self.__class__.PROP_MESSAGES, []):
#getProp() is trying to retreive the list of (jid, message) tuples, if none exist, use the default []
phones = ['phone numbers']
message = 'text message'
for phone in phones:
messageEntity = TextMessageProtocolEntity(message, to = Jid.normalize(phone))
#append the id of message to ackQueue list
#which the id of message will be deleted when ack is received.
self.ackQueue.append(messageEntity.getId())
self.toLower(messageEntity)
self.lock.release()
#after receiving the message from the target number, target number will send a ack to sender(us)
@ProtocolEntityCallback("ack")
def onAck(self, entity):
self.lock.acquire()
#if the id match the id in ackQueue, then pop the id of the message out
if entity.getId() in self.ackQueue:
self.ackQueue.pop(self.ackQueue.index(entity.getId()))
if not len(self.ackQueue):
self.lock.release()
logger.info("Message sent")
#raise KeyboardInterrupt()
self.lock.release()
创建常规堆栈。
class YowsupStack(object):
def __init__(self, credentials, messages, encryptionEnabled = True):
"""
:param credentials:
:param messages: list of (jid, message) tuples
:param encryptionEnabled:
:return:
"""
stackBuilder = YowStackBuilder()
self.stack = stackBuilder\
.pushDefaultLayers(encryptionEnabled)\
.push(SendLayer)\
.push(EchoLayer)\
.build()
self.stack.setProp(SendLayer.PROP_MESSAGES, messages)
self.stack.setProp(YowAuthenticationProtocolLayer.PROP_PASSIVE, True)
self.stack.setCredentials(credentials)
def start(self):
self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
try:
self.stack.loop()
except AuthError as e:
print("Authentication Error: %s" % e.message)
创建echo_client。
class EchoLayer(YowLayer):
def receive(self, protocolEntity):
if protocolEntity.getTag() == "message":
self.onMessage(protocolEntity)
def onMessage(self, messageProtocolEntity):
income_message = messageProtocolEntity.getBody()
print('income message = ', income_message,'tel number = ', messageProtocolEntity.getFrom())
if income_message == 'condition':
outgoingMessageProtocolEntity = TextMessageProtocolEntity(
messageProtocolEntity.getFrom(),
to = 'phone number'
)
self.toLower(outgoingMessageProtocolEntity)
credentials = ("###", "pw")
text_messages= ['messages']
stack = YowsupStack(credentials,mess)
stack.start()