You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							142 lines
						
					
					
						
							5.5 KiB
						
					
					
				
			
		
		
		
			
			
			
		
		
	
	
							142 lines
						
					
					
						
							5.5 KiB
						
					
					
				
								# -*- coding: utf-8 -*-
							 | 
						|
								#############################################################################
							 | 
						|
								#
							 | 
						|
								#    Cybrosys Technologies Pvt. Ltd.
							 | 
						|
								#
							 | 
						|
								#    Copyright (C) 2022-TODAY Cybrosys Technologies(<https://www.cybrosys.com>)
							 | 
						|
								#    Author: Cybrosys Techno Solutions(<https://www.cybrosys.com>)
							 | 
						|
								#
							 | 
						|
								#    You can modify it under the terms of the GNU LESSER
							 | 
						|
								#    GENERAL PUBLIC LICENSE (LGPL v3), Version 3.
							 | 
						|
								#
							 | 
						|
								#    This program is distributed in the hope that it will be useful,
							 | 
						|
								#    but WITHOUT ANY WARRANTY; without even the implied warranty of
							 | 
						|
								#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
							 | 
						|
								#    GNU LESSER GENERAL PUBLIC LICENSE (LGPL v3) for more details.
							 | 
						|
								#
							 | 
						|
								#    You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
							 | 
						|
								#    (LGPL v3) along with this program.
							 | 
						|
								#    If not, see <http://www.gnu.org/licenses/>.
							 | 
						|
								#
							 | 
						|
								#############################################################################
							 | 
						|
								
							 | 
						|
								from odoo import fields, models
							 | 
						|
								import requests, json
							 | 
						|
								
							 | 
						|
								
							 | 
						|
								class ResCompanyInherited(models.Model):
							 | 
						|
								    _inherit = 'res.company'
							 | 
						|
								    _description = 'Slack Api'
							 | 
						|
								
							 | 
						|
								    token = fields.Char(required=True, string="Slack Token")
							 | 
						|
								    slack_users_ids = fields.One2many('res.company.slack.users',
							 | 
						|
								                                      'res_company_id',
							 | 
						|
								                                      "All Users")
							 | 
						|
								    slack_channel_ids = fields.One2many('res.company.slack.channels',
							 | 
						|
								                                        'res_company_id',
							 | 
						|
								                                        "Channels")
							 | 
						|
								    slack_sync = fields.Boolean(default=False)
							 | 
						|
								
							 | 
						|
								    def sync_conversations(self):
							 | 
						|
								        """To sync channel and members from slack"""
							 | 
						|
								        self.slack_sync = True
							 | 
						|
								        url1 = "https://slack.com/api/users.list"
							 | 
						|
								        url2 = "https://slack.com/api/conversations.list"
							 | 
						|
								        url3 = "https://slack.com/api/users.info?user="
							 | 
						|
								        # url4 = "https://slack.com/api/conversations.members"
							 | 
						|
								
							 | 
						|
								        payload = {}
							 | 
						|
								        headers = {
							 | 
						|
								            'Authorization': 'Bearer ' + self.token
							 | 
						|
								        }
							 | 
						|
								        users_response = requests.request("GET", url1, headers=headers,
							 | 
						|
								                                          data=payload)
							 | 
						|
								        users_response = users_response.__dict__['_content']
							 | 
						|
								        dict_users = users_response.decode("UTF-8")
							 | 
						|
								        users = json.loads(dict_users)
							 | 
						|
								        channel_list = []
							 | 
						|
								        channel_response = requests.request("GET", url2, headers=headers,
							 | 
						|
								                                            data=payload)
							 | 
						|
								        channels_response = channel_response.__dict__['_content']
							 | 
						|
								        dict_channels = channels_response.decode("UTF-8")
							 | 
						|
								        channels = json.loads(dict_channels)
							 | 
						|
								
							 | 
						|
								        search_channel = self.env['mail.channel'].search([])
							 | 
						|
								        search_channel.sync_members()
							 | 
						|
								        self.env['res.users'].search([]).sync_users()
							 | 
						|
								        for i in search_channel:
							 | 
						|
								            channel_list.append(i.channel)
							 | 
						|
								        for channel in channels['channels']:
							 | 
						|
								            if channel['id'] not in channel_list:
							 | 
						|
								                self.env['mail.channel'].create([
							 | 
						|
								                    {
							 | 
						|
								                        'name': channel['name'],
							 | 
						|
								                        'channel': channel['id'],
							 | 
						|
								                        'is_slack': True,
							 | 
						|
								                    }, ])
							 | 
						|
								
							 | 
						|
								        users_list = []
							 | 
						|
								        slack_id_list = []
							 | 
						|
								        slack_internal_user_list = []
							 | 
						|
								        members_list = []
							 | 
						|
								        channels_list = []
							 | 
						|
								        record_channel_list = []
							 | 
						|
								        record = self.env.user.company_id
							 | 
						|
								        for in_channel in self.slack_channel_ids:
							 | 
						|
								            record_channel_list.append(in_channel.name)
							 | 
						|
								        for channel in channels['channels']:
							 | 
						|
								            if channel['name'] not in record_channel_list:
							 | 
						|
								                channels_list.append((0, 0,
							 | 
						|
								                                      {'name': channel['name']}
							 | 
						|
								                                      ))
							 | 
						|
								        self.slack_channel_ids = channels_list
							 | 
						|
								        record_user_list = []
							 | 
						|
								        for rec in record.slack_users_ids:
							 | 
						|
								            record_user_list.append(rec.user)
							 | 
						|
								        for rec in users['members']:
							 | 
						|
								            # if rec['is_email_confirmed'] is True:
							 | 
						|
								            if 'email' in rec['profile']:
							 | 
						|
								                email = rec['profile']['email']
							 | 
						|
								            else:
							 | 
						|
								                email = ''
							 | 
						|
								            if rec['id'] not in record_user_list:
							 | 
						|
								                users_list.append((0, 0,
							 | 
						|
								                                   {'name': rec['real_name'],
							 | 
						|
								                                    'user': rec['id'],
							 | 
						|
								                                    'email': email},
							 | 
						|
								                                   ))
							 | 
						|
								        self.slack_users_ids = users_list
							 | 
						|
								
							 | 
						|
								        for partner_id in self.env['res.partner'].search([]):
							 | 
						|
								            slack_id_list.append(partner_id.slack_user_id)
							 | 
						|
								        for rec in users['members']:
							 | 
						|
								            # if rec['is_email_confirmed'] is True:
							 | 
						|
								            if 'email' in rec['profile']:
							 | 
						|
								                email = rec['profile']['email']
							 | 
						|
								            else:
							 | 
						|
								                email = ''
							 | 
						|
								            if rec['id'] not in slack_id_list:
							 | 
						|
								                vals = {
							 | 
						|
								                    'name': rec['real_name'],
							 | 
						|
								                    'slack_user_id': rec['id'],
							 | 
						|
								                    'is_slack_user': True,
							 | 
						|
								                    'email': email
							 | 
						|
								                }
							 | 
						|
								                self.env['res.partner'].create(vals)
							 | 
						|
								
							 | 
						|
								
							 | 
						|
								class SlackUsersLines(models.Model):
							 | 
						|
								    _name = 'res.company.slack.users'
							 | 
						|
								    _description = 'Slack users'
							 | 
						|
								
							 | 
						|
								    name = fields.Char(string="Name")
							 | 
						|
								    email = fields.Char(string="Email")
							 | 
						|
								    user = fields.Char(string="User ID")
							 | 
						|
								    res_company_id = fields.Many2one("res.company")
							 | 
						|
								
							 | 
						|
								
							 | 
						|
								class SlackChannelLines(models.Model):
							 | 
						|
								    _name = 'res.company.slack.channels'
							 | 
						|
								    _description = 'Slack Channels'
							 | 
						|
								    name = fields.Char(string="Name")
							 | 
						|
								    res_company_id = fields.Many2one("res.company")
							 | 
						|
								
							 |