1

I am trying to parse arguments in python code sent from slack... what does this mean is when someone types a command in slack I want to differentiate based on string and other args.

for eg in slack if I use !command inc-number some string and then sometimes !command inc-number word how could I differentiate both

===============================================================================

def update(self,*args):

    inc = args[0]
    id = self.getincsysid(inc) # this func gets the sysid of inc to be update in servicenow.

    request = 'api/now/table/incident/'
    service_now_url = service now url

    url = service_now_url + request + id
    headers = {"Accept": "application/json"}

    # I am stuck here how to differentiate if args1 is a string.
    if args[1] == 'string': #do below 
        requests.put(url, auth=(user, pwd), headers=headers, json= 
    {'comments': args})
        return 'inc updated'
    elif args[1]=='word':
        impact = 'imapct1'
        criticality= 'urgency1'
        requests.put(url, auth=(user, pwd), headers=headers, json={'impact': 
    impact, 'criticality': urgency})

    else:
        return 'none matched'
Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
Muks
  • 63
  • 2
  • 9
  • 1
    If you're asking how you can differentiate **a string** from **a word**, you can't (unless you have some other info): can we refine the question as differentiate **a string with spaces** and **a string without spaces**? – Gsk Jul 05 '18 at 13:31
  • so what i am sending from slack is !command incnumber work done (this is going to notes field)and in another case, I am doing !command incnumber p1 – Muks Jul 05 '18 at 13:37

3 Answers3

0

If you're testing if args[1] is a string use

if type(args[1]) = str:
jc1850
  • 1,101
  • 7
  • 16
0

It is pretty difficult to differentiate a word from a string, mainly because a word IS a string.
If we take as an example for a word the string "p1" and an example for a phrase the string "work done", we may define a word as a set of one or more characters not containing a space, and a phrase as a set of words separed by a space.

Starting by this assumption, our args[1] is a phrase if contains at least one space (we should also verify that it contains at least 2 characters...), else it's a word if it contains at least 1 character, else is something wrong:

if " " in args[1]: #do below 
    requests.put(url, auth=(user, pwd), headers=headers, json= 
{'comments': args})
    return 'inc updated'
elif len(args[1])>0:
    impact = 'imapct1'
    criticality= 'urgency1'
    requests.put(url, auth=(user, pwd), headers=headers, json={'impact': 
impact, 'criticality': urgency})

else:
    return 'none matched'
Gsk
  • 2,929
  • 5
  • 22
  • 29
0

Slack will always return the complete user input as one big string. So you need to implement a parse to transform that string into arguments in accordance with your syntax design.

They are many ways to approach this. I usually use a syntax where a space is used as delimiter to identify each argument plus " can be used to encapsulate a longer sentence with multiple words as one argument.

I guess your best approach would be to use existing parsers. e.g. shlex to parse the input from Slack.

See also this answer for examples on how shlex works.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • splitting arg is not a issue , i am facing issue with how to tell my function that sometime fargs will be a string sometimes it can be just a letter or say a word. – Muks Jul 06 '18 at 05:03