Feature #88403

Connect OTRS spam queue to Rspamd

Added by Andri Steiner 2 months ago. Updated 2 months ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Target version:
-
Start date:
2019-05-20
Due date:
2019-05-20
% Done:

100%


Description

mails manually moved into the OTRS spam queue must be learning by Rspamd

History

#1 Updated by Andri Steiner 2 months ago

  • Status changed from New to Closed
  • % Done changed from 0 to 100

Manually configured within root's crontab on OTRS server srv187

#2 Updated by Michael Stucki 2 months ago

srv187 ➜  ~ cat spamlearning.py
#!/usr/bin/env python

# Modules
import requests
import json
import re

# Configuration
OTRS_API='https://<otrs>/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/'
OTRS_USER='<username>'
OTRS_PASSWORD='<password>'
RSPAMD_API='https://<mail>/rspamd/learnspam'
RSPAMD_PASSWORD='<password>'
RSPAMD_HEADER= { 'password': RSPAMD_PASSWORD }

# get spam queue tickets
otrsApiOptions = {
  'UserLogin': OTRS_USER,
  'Password': OTRS_PASSWORD,
  'Queues': 'Spam',
}
r = requests.get(url = OTRS_API + 'Ticket', params = otrsApiOptions) 
if not r.status_code == requests.codes.ok:
  print ( 'Error: could not fetch tickets' )
  exit ( 1 )

# extracting data in json format 
tickets = r.json() 

# loop through tickets
for ticketID in tickets['TicketID']:
  otrsApiOptions = {
    'UserLogin': OTRS_USER,
    'Password': OTRS_PASSWORD,
    'AllArticles': 1,
  }
  r = requests.get(url = OTRS_API + 'Ticket/' + ticketID, params = otrsApiOptions) 
  if not r.status_code == requests.codes.ok:
    print ( 'Error: could not fetch tickets' )
    exit ( 1 )
  ticket = r.json()
  ticketNumber = ticket['Ticket'][0]['TicketNumber']
  ticketTitle = ticket['Ticket'][0]['Title']
  ticketBody = ticket['Ticket'][0]['Article'][0]['Body']

  # send body to Rspamd
  r = requests.post(url = RSPAMD_API, headers = RSPAMD_HEADER, data = ticketBody.encode('utf-8' ) )
  print ( 'Ticket ' + str ( ticketNumber ) + '] ' + r.reason )

Also available in: Atom PDF