class VKRequestHandler: _instance = None def __init__(self): vk_session = vk_api.VkApi(token=settings.VK_BOT.get('API_TOKEN')) self.vk_client = vk_session self.state_machine = StateMachine(initial_state=BootStrapState()) self.p_msg = '' @classmethod def create_instance(cls): if cls._instance is None: cls._instance = VKRequestHandler() return cls._instance def parse(self, request): try: data = json.loads(request.body) if data['type'] == 'confirmation': return HttpResponse(settings.VK_BOT.get('CONFIRMATION_TOKEN')) if data['type'] != 'message_new' or self.p_msg == request.body: return HttpResponse() self.p_msg = request.body trigger = create_trigger_from_request(data, self.vk_client) self.state_machine.fire(trigger) except Exception as e: logger.warning(f'receive invalid request. {e}') return HttpResponse('ok')
class ViberRequestHandler: _instance = None def __init__(self): bot_config = _get_bot_configuration() self.viber_client = Api(bot_config) self.state_machine = StateMachine(initial_state=BootStrapState()) @classmethod def create_instance(cls): if cls._instance is None: cls._instance = ViberRequestHandler() return cls._instance def parse(self, request): viber_request = self.viber_client.parse_request(request.body) if isinstance(viber_request, (ViberSubscribedRequest, ViberMessageRequest)): trigger = create_trigger_from_request(viber_request, self.viber_client) # получение пользовательского id - viber_request.sender.id # получение время обращения timestamp - viber_request.timestamp # сохранение статистики self.state_machine.fire(trigger) elif isinstance(viber_request, ViberFailedRequest): pass
def start(bot, update): machine = StateMachine(initial_state=BootStrapState()) try: usr = User.objects.get(user_id=update.message.chat_id).state except Exception as e: usr = None trigger = TelegramTrigger(client=bot, user_id=update.message.chat_id, messenger=0, text=update.message.text, user_state=usr) machine.fire(trigger)
class FacebookRequestHandler: _instance = None def __init__(self): self.facebook_client = Bot(settings.FACEBOOK_BOT.get('ACCESS_TOKEN')) self.state_machine = StateMachine(initial_state=BootStrapState()) @classmethod def create_instance(cls): if cls._instance is None: cls._instance = FacebookRequestHandler() return cls._instance def parse(self, request): try: if request.method == 'GET': """Before allowing people to message your bot, Facebook has implemented a verify token that confirms all requests that your bot receives came from Facebook.""" token_sent = request.GET['hub.verify_token'] if token_sent == settings.FACEBOOK_BOT.get('VERIFY_TOKEN'): return HttpResponse(request.GET['hub.challenge']) else: data = json.loads(request.body) for event in data['entry']: messaging = event['messaging'] for x in messaging: if x.get('message') and x['message'].get('text'): trigger = create_trigger_from_request( x, self.facebook_client) self.state_machine.fire(trigger) pass except Exception as e: traceback.print_exc() logger.warning(f'receive invalid request. {e}') return HttpResponse()
def __init__(self): self.facebook_client = Bot(settings.FACEBOOK_BOT.get('ACCESS_TOKEN')) self.state_machine = StateMachine(initial_state=BootStrapState())
def __init__(self): vk_session = vk_api.VkApi(token=settings.VK_BOT.get('API_TOKEN')) self.vk_client = vk_session self.state_machine = StateMachine(initial_state=BootStrapState()) self.p_msg = ''
def __init__(self): bot_config = _get_bot_configuration() self.viber_client = Api(bot_config) self.state_machine = StateMachine(initial_state=BootStrapState())