From b5056d6a00bb8ca3f450e512bb97535864344c74 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Thu, 7 Jul 2016 13:34:04 +0300 Subject: [PATCH] bot v0.1 --- Bot/bot.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Bot/bot.py diff --git a/Bot/bot.py b/Bot/bot.py new file mode 100644 index 0000000..d95f888 --- /dev/null +++ b/Bot/bot.py @@ -0,0 +1,60 @@ +import plugin_super_class +from PySide import QtCore +import time + + +class InvokeEvent(QtCore.QEvent): + EVENT_TYPE = QtCore.QEvent.Type(QtCore.QEvent.registerEventType()) + + def __init__(self, fn, *args, **kwargs): + QtCore.QEvent.__init__(self, InvokeEvent.EVENT_TYPE) + self.fn = fn + self.args = args + self.kwargs = kwargs + + +class Invoker(QtCore.QObject): + + def event(self, event): + event.fn(*event.args, **event.kwargs) + return True + +_invoker = Invoker() + + +def invoke_in_main_thread(fn, *args, **kwargs): + QtCore.QCoreApplication.postEvent(_invoker, InvokeEvent(fn, *args, **kwargs)) + + +class Bot(plugin_super_class.PluginSuperClass): + + def __init__(self, *args): + super(Bot, self).__init__('Bot', 'bot', *args) + self._callback = None + self._timer = QtCore.QTimer() + self._timer.timeout.connect(self.initialize) + + def start(self): + self._timer.start(10000) + + def initialize(self): + self._timer.stop() + self._callback = self._tox.friend_message_cb + + def incoming_message(tox, friend_number, message_type, message, size, user_data): + self._callback(tox, friend_number, message_type, message, size, user_data) + if self._profile.status == 1: + self.answer(friend_number, str(message, 'utf-8')) + + self._tox.callback_friend_message(incoming_message, None) + + def stop(self): + self._tox.callback_friend_message(self._callback, None) + + def close(self): + self.stop() + + def answer(self, friend_number, message): + invoke_in_main_thread(self._profile.send_message, message, friend_number) + +