def run(self, current_use): no_errors = True try: response = self.func(current_use) except: no_errors = False current_use.traceback = get_exc_info() response = 'Error while <{}> execution\n'.format(self.name) if not current_use.in_conversation: if current_use.user.is_admin: response += current_use.traceback if response is None: return no_errors if type(response) != dict: response = {'message': str(response)} if CONFIG.FORWARD and not current_use._responses and \ not current_use.in_conversation and not current_use.is_action: if 'forward_messages' not in response: response['forward_messages'] = current_use.msg_id # last_msg_text = response['message'] # todo send big by parts current_use.send_msg(response) return no_errors
def answer_callback_query(self, text): if self.messenger.name != 'TG': return 0 text = str(text) try: resp = telebot.apihelper.answer_callback_query( CONFIG.TG_BOT_TOKEN, self.msg_id, text) return 1 except: self.error_log_add('Answering', request=text, additional=get_exc_info()) return 0
def deploy_bots(chat_id_my): bots = Bot.objects.all() try: obj = Chat.objects.get(id_my=chat_id_my) exists = 1 except Chat.DoesNotExist: exists = 0 if exists: return 0 code = ''' var chat_id_my = "''' + str(chat_id_my) + '''"; var bots = [''' + ','.join([str(bot.id) for bot in bots]) + ''']; var i = 0; while (i < bots.length) { API.messages.addChatUser({"chat_id":chat_id_my, "user_id": bots[i]}); // allbots i = i + 1; }''' payload = { 'code': code, } vk_session = VkApi(token=global_config.VK_MY_TOKEN) try: resp = vk_session.method('execute', payload) except: print(get_exc_info()) return 0 bots_dt = {} for bot in bots: bot.latest_chat += 1 bot.save(update_fields=['latest_chat']) bots_dt[bot.id] = { 'access_token': bot.access_token, 'chat_id': bot.latest_chat } bot_session = VkApi(token=bot.access_token) payload = {'chat_id': bot.latest_chat, 'user_id': bot.id} resp = bot_session.method('messages.removeChatUser', payload) chat = Chat(id_my=chat_id_my, bots_data=json.dumps(bots_dt), antikick=False) chat.save() # chats.append(chat) # chats = Chat.objects.all() return 1
def _send(self, data): if type(data) != dict: data = {'message': str(data)} self.current_data = data.copy() self.history.append(self.current_data) try: msg_id = self._request_to_send(data) return msg_id except: self.error_log_add('Sending message', request=data, additional=get_exc_info()) return -1
def edit(self, data, log=True): if type(data) != dict: data = {'message': str(data)} if log: self.current_data = data.copy() self.history.append(self.current_data) try: return self._request_to_edit(data) except: self.error_log_add('Editing message', request=data, additional=get_exc_info()) return False
async def update_lp_url(group_id, client_session): group = groups_list[group_id] group['active'] = 0 print('updating lp_url', group_id) req_url = 'https://api.vk.com/method/groups.getLongPollServer?group_id={id}&access_token={token}&v=5.101'.format( id=abs(group_id), token=group['access_token']) task = asyncio.ensure_future(fetch(req_url, client_session)) resp = await asyncio.gather(task) add_data, resp = resp[0] resp = json.loads(resp) try: resp = resp['response'] lp_url = '{server}?act=a_check&key={key}&wait=90&mode=2&ts='.format( **resp) except BaseException as exc: error = resp['error'] error_code = error['error_code'] if error_code == 27: group_obj = group['group_obj'] group_obj.is_blocked = True group_obj.save(update_fields=['is_blocked']) groups_list.pop(group_id) print('Group', group_id, 'is BLOCKED :(') return 0 text = '{}:{}'.format(group_id, get_exc_info()) print(text) notify(text) lp_url = None if not lp_url: return 0 group['ts'] = resp['ts'] group['lp_url'] = lp_url group['active'] = 1 return 1
def process_exception(self, request, exception): exc_info = get_exc_info() message = exc_info notify(message) return None
response = agr_handlers[agr_level](response) return response try: sess = VkApi(token=VK_MY_TOKEN) exec_stack = ExecuteStack(sess) payload = {'user_ids': uid, 'fields': 'online'} is_online = sess.method('users.get', payload)[0]['online'] if is_online and (time.time() - storage['latest_send']) > SEND_TIMEOUT: storage['latest_send'] = time.time() actions = gen_actions() for action in actions: exec_stack.add(action['method'], action['payload'], additional=action['callback']) resps = exec_stack.finish() for resp, callback in resps: if callback: callback(resp) storage['current_notifies'] += 1 if storage['current_notifies'] == 1: storage['current_first'] = time.time() except: notify('sanya_sleep error\n' + get_exc_info()) storage.save()