A
A
Arseny Sokolov2016-02-02 19:49:13
Python
Arseny Sokolov, 2016-02-02 19:49:13

How to call the sendMessage function from another class in Twisted?

There is this code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ICQ bot

import os
import re
import sys
import urllib2
import feedparser
from twisted.words.protocols import oscar
from twisted.internet import protocol, reactor

# UIN
UIN = "658127246"
PASS = "[email protected]$$w0rd"

# Server
host = ("login.icq.com", 5190)
icqMode = 1

# Status message
AMSG = "I'm here +)"


class Echo(protocol.Protocol):
    def dataReceived(self, data):
        print data
        try:
            finded = re.findall(r"(?P<name>.*?): '(?P<value>.*?)'; ", data)
            data2 = dict(finded)
            sendTo = data2['SendTo']
            msg = data2['MSG']
        except:
            sendTo = '881129'
            msg = '=> error receive data message <='
        response = 'Send to UIN: %s\r\nMessage: %s\r\n\r\n' % (sendTo, msg)
        self.transport.write(response)
        # Как от сюда вызвать sendMessage(sendTo, msg) ?????


class B(oscar.BOSConnection):

    capabilities = [oscar.CAP_CHAT]

    def initDone(self):
        print "Connect ",UIN," to server", host[0], host[1]
        self.requestSelfInfo().addCallback(self.gotSelfInfo)
        self.requestSSI().addCallback(self.gotBuddyList)
        self.setAway(AMSG)
        factory = protocol.ServerFactory()
        factory.protocol = Echo
        reactor.listenTCP(8007, factory)

    def gotSelfInfo(self, user):
        print user.__dict__
        self.name = user.name

    def gotBuddyList(self, l):
        print l
        self.activateSSI()
        self.setProfile("""ICQBot""")
        self.setIdleTime(0)
        self.clientReady()
        # self.sendICQMessage()

    def gotAway(self, away, user):
        if away:
            print "User ", user,": ",away
 

class OA(oscar.OscarAuthenticator):
    print 'Start ICQ connection'
    print 'pid: ', os.getpid()
    BOSClass = B


if __name__ == '__main__':
    protocol.ClientCreator(reactor, OA, UIN, PASS, icq=icqMode).connectTCP(*host)
    # reactor.callLater(10, reactor.stop)
    reactor.run()

    sys.exit()

I don't know how to call the method sendMessage. Banal B().sendMessage()throws an error:
exceptions.TypeError: __init__() takes exactly 3 arguments (1 given)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oscar Django, 2016-02-03
@winordie

I am not familiar with twisted, so I don’t know how to do it correctly, but given that twisted is python: with the
line
you are trying to call an instance method of class B, immediately initializing it.
Considering that B inherits from oscar.BOSConnection, look here and see that for initialization, you need to pass username and cookie, i.e. something like this:
B(username, cookie).sendMessage()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question