|
@ -19,7 +19,7 @@ |
|
|
# If not, see <http://www.gnu.org/licenses/>. |
|
|
# If not, see <http://www.gnu.org/licenses/>. |
|
|
# |
|
|
# |
|
|
############################################################################# |
|
|
############################################################################# |
|
|
from odoo import fields, models, api |
|
|
from odoo import api, fields, models |
|
|
import requests, re, json |
|
|
import requests, re, json |
|
|
from datetime import datetime |
|
|
from datetime import datetime |
|
|
from dateutil import tz |
|
|
from dateutil import tz |
|
@ -35,7 +35,7 @@ class MailMessage(models.Model): |
|
|
_description = 'Slack Message' |
|
|
_description = 'Slack Message' |
|
|
|
|
|
|
|
|
is_slack = fields.Boolean(string="Slack", default=False) |
|
|
is_slack = fields.Boolean(string="Slack", default=False) |
|
|
channel = fields.Many2one('mail.channel') |
|
|
channel = fields.Many2one('mail.channel', string="Channel") |
|
|
|
|
|
|
|
|
@api.model |
|
|
@api.model |
|
|
def create(self, vals): |
|
|
def create(self, vals): |
|
@ -57,9 +57,12 @@ class MailMessage(models.Model): |
|
|
} |
|
|
} |
|
|
new_text = remove_html(rec.body) |
|
|
new_text = remove_html(rec.body) |
|
|
url = "https://slack.com/api/chat.postMessage?channel=" + rec.record_name + "&" + "text=" + new_text |
|
|
url = "https://slack.com/api/chat.postMessage?channel=" + rec.record_name + "&" + "text=" + new_text |
|
|
requests.request("POST", url, headers=headers, data=payload) |
|
|
requests.request("POST", url, headers=headers, |
|
|
|
|
|
data=payload) |
|
|
rec.is_slack = True |
|
|
rec.is_slack = True |
|
|
converted_time = datetime.strptime(channel_date,'%Y-%m-%d %H:%M:%S').astimezone(to_zone) |
|
|
converted_time = datetime.strptime(channel_date, |
|
|
|
|
|
'%Y-%m-%d %H:%M:%S').astimezone( |
|
|
|
|
|
to_zone) |
|
|
channel.msg_date = converted_time.strftime('%Y-%m-%d %H:%M:%S') |
|
|
channel.msg_date = converted_time.strftime('%Y-%m-%d %H:%M:%S') |
|
|
return res |
|
|
return res |
|
|
|
|
|
|
|
@ -74,41 +77,45 @@ class MailMessage(models.Model): |
|
|
payload = {} |
|
|
payload = {} |
|
|
headers = {'Authorization': 'Bearer ' + company_record.token} |
|
|
headers = {'Authorization': 'Bearer ' + company_record.token} |
|
|
url = "https://slack.com/api/conversations.history?channel=" + channel_id.channel |
|
|
url = "https://slack.com/api/conversations.history?channel=" + channel_id.channel |
|
|
channel_response = requests.request("GET", url, headers=headers, |
|
|
channel_response = requests.request("GET", url, |
|
|
|
|
|
headers=headers, |
|
|
data=payload) |
|
|
data=payload) |
|
|
channels_response = channel_response.__dict__['_content'] |
|
|
channels_response = channel_response.__dict__['_content'] |
|
|
dict_channels = channels_response.decode("UTF-8") |
|
|
dict_channels = channels_response.decode("UTF-8") |
|
|
channels_history = json.loads(dict_channels) |
|
|
channels_history = json.loads(dict_channels) |
|
|
channels_history['messages'].reverse() |
|
|
if channels_history['ok']: |
|
|
if not channel_id.msg_date: |
|
|
channels_history['messages'].reverse() |
|
|
converted_time = datetime.strptime(channel_date, |
|
|
if not channel_id.msg_date: |
|
|
'%Y-%m-%d %H:%M:%S').astimezone( |
|
|
converted_time = datetime.strptime(channel_date, |
|
|
to_zone) |
|
|
'%Y-%m-%d %H:%M:%S').astimezone( |
|
|
channel_id.msg_date = converted_time.strftime( |
|
|
to_zone) |
|
|
'%Y-%m-%d %H:%M:%S') |
|
|
channel_id.msg_date = converted_time.strftime( |
|
|
else: |
|
|
'%Y-%m-%d %H:%M:%S') |
|
|
for i in channels_history['messages']: |
|
|
else: |
|
|
if 'user' in i: |
|
|
for i in channels_history['messages']: |
|
|
users = self.env['res.users'].search( |
|
|
if 'user' in i: |
|
|
[('slack_user_id', '=', i['user'])]) |
|
|
users = self.env['res.users'].search( |
|
|
dt_object = ( |
|
|
[('slack_user_id', '=', i['user'])]) |
|
|
datetime.fromtimestamp(float(i['ts'])).strftime( |
|
|
dt_object = ( |
|
|
'%Y-%m-%d %H:%M:%S')) |
|
|
datetime.fromtimestamp( |
|
|
date_time_obj = datetime.strptime(dt_object, |
|
|
float(i['ts'])).strftime( |
|
|
'%Y-%m-%d %H:%M:%S') |
|
|
'%Y-%m-%d %H:%M:%S')) |
|
|
converted_time = date_time_obj.astimezone(to_zone) |
|
|
date_time_obj = datetime.strptime(dt_object, |
|
|
if datetime.strptime(converted_time.strftime( |
|
|
'%Y-%m-%d %H:%M:%S') |
|
|
'%Y-%m-%d %H:%M:%S'), |
|
|
converted_time = date_time_obj.astimezone( |
|
|
'%Y-%m-%d %H:%M:%S') > channel_id.msg_date: |
|
|
to_zone) |
|
|
self.with_user(users.id).create({ |
|
|
if datetime.strptime(converted_time.strftime( |
|
|
'body': i['text'], |
|
|
'%Y-%m-%d %H:%M:%S'), |
|
|
'record_name': channel_id.name, |
|
|
'%Y-%m-%d %H:%M:%S') > channel_id.msg_date: |
|
|
'model': 'mail.channel', |
|
|
self.with_user(users.id).create({ |
|
|
'is_slack': True, |
|
|
'body': i['text'], |
|
|
'res_id': channel_id.id, |
|
|
'record_name': channel_id.name, |
|
|
}) |
|
|
'model': 'mail.channel', |
|
|
converted_time = datetime.strptime(channel_date, |
|
|
'is_slack': True, |
|
|
'%Y-%m-%d %H:%M:%S').astimezone( |
|
|
'res_id': channel_id.id, |
|
|
to_zone) |
|
|
}) |
|
|
channel_id.msg_date = converted_time.strftime( |
|
|
converted_time = datetime.strptime(channel_date, |
|
|
'%Y-%m-%d %H:%M:%S') |
|
|
'%Y-%m-%d %H:%M:%S').astimezone( |
|
|
|
|
|
to_zone) |
|
|
|
|
|
channel_id.msg_date = converted_time.strftime( |
|
|
|
|
|
'%Y-%m-%d %H:%M:%S') |
|
|