Reputation: 193
I'm trying to make a function to send a message to a specific user for this in my model I wrote this code
class SkypeBot(models.Model):
_name = 'my.skype'
_inherit = ['mail.thread', 'mail.activity', 'res.users']
_description = 'My Skype'
skype_login = fields.Char('Your skype Login')
skype_password = fields.Char('Your skype password')
skype_message = fields.Char(store=True)
@api.multi
def msg(self, message):
partner_id = self.env['res.users'].search([('id', '=', 2)]).partner_id.id
_logger.info('^^^^^' * 5)
_logger.warning(partner_id)
_logger.info('^^^^^' * 5)
self.env['mail.message'].create({'message_type': 'notification',
'subtype': self.env.ref('mail.mt_comment').id, # subject type
'body': message,
'subject': 'Message subject',
'partner_ids': [(4, partner_id), ],
# partner to whom you send notification
})
I want to call this method (msg) from another MySkype class:
class MySkype(skpy.SkypeEventLoop):
def onEvent(self, event):
if isinstance(event, skpy.SkypeNewMessageEvent):
message = ('New message from user {} at {}: \'{} \''.format(event.msg.userId,
event.msg.time.strftime('%H:%M dd. %d.%m.%Y'),
event.msg.content))
_logger.info('--------'*5)
_logger.warning(event)
_logger.info('--------' * 5)
_logger.warning(message)
_logger.info('--------' * 5)
skype_model.SkypeBot.msg(message=message)
from skpy import Skype
sk = MySkype('+375', '1239qW', autoAck=True)
thread = threading.Thread(target=sk.loop)
thread.start()
but when this method is called I get an error
лют 10 10:42:00 PK odoo12[8534]: 2019-02-10 07:42:00,618 8534 INFO ? odoo.addons.bus.models.bus: Bus.loop listen imbus on db postgres
лют 10 10:42:50 PK odoo12[8534]: 2019-02-10 07:42:50,620 8534 INFO odoo12 werkzeug: 127.0.0.1 - - [10/Feb/2019 07:42:50] "POST /longpolling/poll HTTP/1.1" 200 - 35 0.030 53.925
лют 10 10:42:55 PK odoo12[8534]: 2019-02-10 07:42:55,056 8534 INFO ? odoo.addons.skype_bot.skype_send_message: —--------------------------------------
лют 10 10:42:55 PK odoo12[8534]: 2019-02-10 07:42:55,056 8534 WARNING ? odoo.addons.skype_bot.skype_send_message: [SkypeNewMessageEvent]
лют 10 10:42:55 PK odoo12[8534]: Id: 1014
лют 10 10:42:55 PK odoo12[8534]: Type: NewMessage
лют 10 10:42:55 PK odoo12[8534]: Time: 2019-02-10 07:42:54
лют 10 10:42:55 PK odoo12[8534]: MsgId: 1549784574906
лют 10 10:42:55 PK odoo12[8534]: 2019-02-10 07:42:55,057 8534 INFO ? odoo.addons.skype_bot.skype_send_message: —--------------------------------------
лют 10 10:42:55 PK odoo12[8534]: 2019-02-10 07:42:55,057 8534 WARNING ? odoo.addons.skype_bot.skype_send_message: Test!Test!Test!Test!Test!
лют 10 10:42:55 PK odoo12[8534]: 2019-02-10 07:42:55,057 8534 INFO ? odoo.addons.skype_bot.skype_send_message: —--------------------------------------
лют 10 10:43:40 PK odoo12[8534]: Exception in thread Thread-2:
лют 10 10:43:40 PK odoo12[8534]: Traceback (most recent call last):
лют 10 10:43:40 PK odoo12[8534]: File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
лют 10 10:43:40 PK odoo12[8534]: self.run()
лют 10 10:43:40 PK odoo12[8534]: File "/usr/lib/python3.6/threading.py", line 864, in run
лют 10 10:43:40 PK odoo12[8534]: self._target(*self._args, **self._kwargs)
лют 10 10:43:40 PK odoo12[8534]: File "/opt/odoo12/odoo-venv/lib/python3.6/site-packages/skpy/main.py", line 207, in loop
лют 10 10:43:40 PK odoo12[8534]: self.cycle()
лют 10 10:43:40 PK odoo12[8534]: File "/opt/odoo12/odoo-venv/lib/python3.6/site-packages/skpy/main.py", line 196, in cycle
лют 10 10:43:40 PK odoo12[8534]: self.onEvent(event)
лют 10 10:43:40 PK odoo12[8534]: File "/opt/odoo12/odoo-custom-addons/skype_bot/skype_send_message.py", line 33, in onEvent
лют 10 10:43:40 PK odoo12[8534]: skype_model.SkypeBot.msg(message=message)
лют 10 10:43:40 PK odoo12[8534]: TypeError: msg() missing 1 required positional argument: 'self'
лют 10 10:43:40 PK odoo12[8534]: 2019-02-10 07:43:40,702 8534 INFO odoo12 werkzeug: 127.0.0.1 - - [10/Feb/2019 07:43:40] "POST /longpolling/poll HTTP/1.1" 200 - 8 0.003 50.051
what am i doing wrong and how do i solve this problem
I tried using @ api.model it did not help me. It seems I have a problem with the initialization of the odoo environment but I can understand exactly where
Upvotes: 0
Views: 2997
Reputation: 1995
As @TigerhawkT3 mentioned, you're calling the method as if it were a static method, rather than a class method.
In Python, the first argument of every class method (including
init
) is always a reference to the current instance of the class. By convention, this argument is always namedself
.¹
In order to call this method properly, you need to create an instance of class SkypeBot
and then call msg()
:
sbot = SkypeBot()
sbot.msg(message)
For more information on the language design decisions that went into the explicit declaration of self
as a parameter, see this Stack Overflow post and this article by Guido van Rossum.
Upvotes: 3