Skip to content

Instantly share code, notes, and snippets.

@wolsen
Created April 20, 2015 22:07
Show Gist options
  • Save wolsen/6e190ebe5b69789425f3 to your computer and use it in GitHub Desktop.
Save wolsen/6e190ebe5b69789425f3 to your computer and use it in GitHub Desktop.
# coding=utf8
"""
salesforce.py - Willie Salesforce Module for displaying
support case information
Copyright 2015, Billy Olsen
Licensed under the Eiffel Forum License 2.
"""
from __future__ import unicode_literals
from willie import module
from willie.formatting import (
bold,
underline,
)
from willie.logger import get_logger
LOGGER = get_logger(__name__)
try:
from simple_salesforce import Salesforce
except ImportError:
Salesforce = None
def configure(config):
if not config.option("Configure Salesforce cases to be displayed", False):
return
config.add_section('salesforce')
config.interactive_add('salesforce', 'username',
'Salesforce username', '')
config.interactive_add('salesforce', 'password',
'Salesforce password', '')
config.interactive_add('salesforce', 'security_token',
'Salesforce api token', '')
def _get_salesforce(bot):
"""
Retrieves the Salesforce instances based on the bot
configuration. If Salesforce is unavailable on this
host or if there is insufficient information this
function will return None.
:param bot: the bot instance, mainly containing config
:returns: Salesforce instance or None
"""
if Salesforce is None:
LOGGING.error("Salesforce could not be located")
return None
config = bot.config.salesforce
try:
return Salesforce(username=config.username,
password=config.password,
security_token=config.security_token)
except:
LOGGER.error("Unable to log into salesforce! Received: %s" %
sys.exc_info()[0])
return None
def _report_case_info(bot, where_clause):
"""
Reports the information for the case based on the where clause
specified.
"""
sf = _get_salesforce(bot)
if not sf:
LOGGER.error("Unable to get a connection to salesforce.")
return
query = ("SELECT Case.Id, Case.CaseNumber, Case.Priority, Case.Subject, "
"Case.Status, Account.Name FROM Case WHERE %s" % where_clause)
try:
results = sf.query(query)
except:
LOGGER.exception("Unable to perform query %s" % query)
results = None
if not results or results.get('totalSize') <= 0:
return
# Only get the first record
records = results.get('records')
LOGGER.error("Found records: %s" % str(records))
if not records or len(records) == 0:
return
record = results.get('records')[0]
status = record.get('Status')
if status == u'Void':
# Invalid record
return
account = record.get('Account')
if account == u'Void':
# Invalid account
return
customer = account.get('Name')
case_id = record.get('Id')
case_num = record.get('CaseNumber')
priority = record.get('Priority')
subject = record.get('Subject')
url = ("https://%s/%s" % (sf.sf_instance, case_id))
message = ("(%(customer)s) %(subject)s "
"[%(priority)s, %(status)s] Case %(case_num)s, %(url)s" %
{'customer': underline(customer),
'subject': subject,
'priority': bold(priority),
'status': bold(status),
'case_num': case_num,
'url': url})
bot.say(message)
@module.rule(r'.*(?i)(?:case|sf|ticket)\s?[:#]?\s?(\d{5,8}).*')
def show_from_case_num(bot, trigger):
"""
Shows salesforce information from a case number.
"""
#bot.say("showing from case num: %s" % trigger.groups(2))
case_num = trigger.groups(2)[0].zfill(8)
where_clause = ("Case.CaseNumber='%s'" % case_num)
_report_case_info(bot, where_clause)
@module.rule(r'.*https?://[\d\w]+.salesforce.com/([\d\w]+).*')
def show_from_link(bot, trigger):
"""
Shows salesforce information from a hyperlink to a specific case.
"""
#bot.say("showing from link: %s" % trigger.groups(2))
case_id = trigger.groups(2)
where_clause = ("Case.Id='%s'" % case_id)
_report_case_info(bot, where_clause)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment