Ejemplo n.º 1
0
    def on_state_1(self, income_message):
        txt = income_message['body']
        if txt.lower() in ('!', 'чат', '"чат"'):
            companion = self.get_new_companion()
            if not companion:
                message = '{wait_companion}\n{to_stop_search}'.format(
                    **self.ru_phrases)
            else:
                message = '{companion_found}\n{to_stop_chat}'.format(
                    **self.ru_phrases)

                safe_call(self.api.messages.send,
                          access_token=companion[0],
                          user_id=companion[1],
                          title=self.ru_phrases['bot_title'],
                          message=message)
        else:
            message = '{to_start_chat}'.format(**self.ru_phrases)

        if self.is_new:
            message += '\n-----\n{new_user_greetings}'.format(
                **self.ru_phrases)
            if not self.in_public():
                message += '\n{to_save_bot}'.format(**self.ru_phrases)

        safe_call(self.api.messages.send,
                  access_token=self.token,
                  user_id=str(self.in_scope_id),
                  title=self.ru_phrases['bot_title'],
                  message=message)
Ejemplo n.º 2
0
    def on_message_allow(self):
        if self.is_new:
            message = self.ru_phrases['allow_new']
        else:
            message = self.ru_phrases['allow_seen']

        safe_call(self.api.messages.send,
                  access_token=self.token,
                  user_id=self.in_scope_id,
                  title=self.ru_phrases['bot_title'],
                  message=message)
Ejemplo n.º 3
0
    def on_group_join(self):
        if self.is_new:
            message = self.ru_phrases['join_new']
        else:
            message = self.ru_phrases['join_seen']

        try:
            safe_call(self.api.messages.send,
                      access_token=self.token,
                      user_id=self.in_scope_id,
                      title=self.ru_phrases['bot_title'],
                      message=message)
        except VkAPIError as e:
            pass
Ejemplo n.º 4
0
    def on_state_3(self, income_message):
        txt = income_message['body']
        companion = self.get_companion()
        if txt.lower() in ('!', 'стоп', '"стоп"'):
            self.close_chat()

            message = '{companion_left}\n{to_start_search}'.format(
                **self.ru_phrases)
            safe_call(self.api.messages.send,
                      access_token=companion[0],
                      user_id=str(companion[1]),
                      title=self.ru_phrases['bot_title'],
                      message=message)
            message = '{user_left}\n{to_start_search}'.format(
                **self.ru_phrases)
            safe_call(self.api.messages.send,
                      access_token=self.token,
                      user_id=self.in_scope_id,
                      title=self.ru_phrases['bot_title'],
                      message=message)
        else:
            safe_call(self.api.messages.markAsRead,
                      access_token=self.token,
                      message_ids=(income_message['id'], ),
                      start_message_id=income_message['id'])

            data_to_send = {
                'user_id': companion[1],
                'access_token': companion[0]
            }

            data_to_send.update(
                self.get_redirected_message(income_message, companion[0]))

            safe_call(self.api.messages.send, **data_to_send)
Ejemplo n.º 5
0
    def on_state_2(self, income_message):
        txt = income_message['body']
        if txt.lower() in ('!', 'стоп', '"стоп"'):
            self.stop_search()
            message = '{search_stopped}\n{to_start_search}'.format(
                **self.ru_phrases)
        else:
            message = '{wait_companion}\n{to_stop_search}'.format(
                **self.ru_phrases)

        safe_call(self.api.messages.send,
                  access_token=self.token,
                  user_id=self.in_scope_id,
                  title=self.ru_phrases['bot_title'],
                  message=message)
Ejemplo n.º 6
0
    def upload_remote_photo(self, url_remote, dist_token):
        def file_by_url(url):
            r = requests.get(url, stream=True)
            if r.status_code == 200:
                r.raw.decode_content = True
                return r.raw

        upload_server = safe_call(self.api.photos.getMessagesUploadServer,
                                  access_token=dist_token)
        upload_url = upload_server['upload_url']

        remote_file = file_by_url(url_remote)

        post_fields = {'photo': ('smth.png', remote_file)}

        response = requests.post(upload_url, files=post_fields)
        file_data = json.loads(response.text)
        file_data['access_token'] = dist_token

        photo = safe_call(self.api.photos.saveMessagesPhoto, **file_data)[0]

        return photo
Ejemplo n.º 7
0
    def get_redirected_message(self, source_message, dist_token):
        message = 'Собеседник: \n{}'.format(source_message['body'])

        message_data = {'title': 'От собеседника', 'message': message}

        if 'fwd_messages' in source_message:
            safe_call(self.api.messages.send,
                      access_token=self.token,
                      user_id=self.in_scope_id,
                      title=self.ru_phrases['bot_title'],
                      message=self.ru_phrases['user_fwd_msg'],
                      forward_messages=(source_message['id'], ))

        sticker = None
        if 'attachments' in source_message:
            self.re_product_link = re.compile(
                '.+vk\.com\/product(\d+)_(\d+)_([^\?]+).+')
            sticker = self.get_sticker_data(source_message['attachments'][0])
            if sticker is None:
                attachments = self.get_valid_attachments(source_message['id'])

                links = self.get_link_urls(attachments)
                if links != []:
                    message_data['message'] += ''.join(
                        ['\nСсылка: ' + link for link in links])

                message_data['attachment'] = self.attachments_to_attachment(
                    attachments)

        if sticker is not None:
            key = self.get_max_photo_key(sticker)
            message_data['message'] = self.ru_phrases['companion_sticker']
            sticker_data = self.upload_remote_photo(sticker[key], dist_token)
            message_data['attachment'] = 'photo{owner_id}_{id}'.format(
                **sticker_data)

        return message_data
Ejemplo n.º 8
0
        int(re.split('\.', f)[0]) for f in listdir(mypath)
        if isfile(join(mypath, f))
    ]
    for group_id in group_ids:
        api = get_api(access_token=tokens[group_id], v=5.25)
        print('got api for', group_id)
        with open('/root/sphere_project/user_ids/{}.json'.format(group_id),
                  'r') as f:
            user_ids = json.load(f)
            print('len(user_ids)', len(user_ids))

            with open('json_histories/{}.json'.format(group_id), 'w') as f_res:
                for i, user_id in enumerate(user_ids):
                    print(i, 'for user', user_id)
                    offset = 0
                    iterate = True
                    user_messages = []
                    while iterate:
                        res = safe_call(api.messages.getHistory,
                                        count=200,
                                        user_id=user_id,
                                        rev=1,
                                        offset=offset)
                        len_items = len(res['items'])
                        user_messages += res['items']
                        iterate = len_items > 0
                        offset += 200
                    f_res.write('{"' + str(user_id) + '":')
                    f_res.write(json.dumps(user_messages))
                    f_res.write('}\n')
Ejemplo n.º 9
0
    def get_valid_attachments(self, message_id):
        res = safe_call(self.api.messages.getById,
                        access_token=self.token,
                        message_ids=(message_id, ))

        return res['items'][0]['attachments']
Ejemplo n.º 10
0
 def in_public(self):
     response = safe_call(self.api.groups.isMember,
                          access_token=self.token,
                          group_id=self.scope_id,
                          user_id=self.in_scope_id)
     return response == 1