Solved

python function call giving bot error

  • 30 January 2022
  • 6 replies
  • 209 views

using community version and open a python script manual input

 

import json

  

def myfunc(mystr: str) -> str:

  data = json.loads(mystr)

  return data['meta']

 

when I call the function myfunc passing a string I get bot error. The string is json returned from an rest api call, when I put the script in a py file and run from desktop command line it works fine.

 

I worked 1 time then it keeps returning bot error. I noticed lots of issues on forum with this type of issue: running python in AAI

icon

Best answer by Ashwin A.K 30 January 2022, 16:34

View original

6 replies

Userlevel 6
Badge +16

Hi @mark fung-a-fat​ ,

 

Can you share your AA Code Snippet?

Chandu

 

import json

  

def myfunc(mystr: str) -> str:

  data = json.loads(mystr)

  return data['meta']

 

this is the code I put in the open script: manual

 

this is the code that I put into the function call and i pass a string that is json that returned from my rest call

 

image 

image

Userlevel 4
Badge +7

Hi @mark fung-a-fat​ ,

 

Could you try the following snippet and see if that works out for you?

 

import json

 

def myfunc(mystr):

 data = json.loads(mystr)

 return data['meta']

 

Kind Regards,

Ashwin A.K

ashwin

this worked thank you.
Userlevel 4
Badge +7

Glad I could help!

 

I'd appreciate it if you marked my solution as "Best" so that others facing similar issues may benefit from it as well.

 

Kind Regards,

Ashwin A.K

Badge +2

Hi @Ashwin A.K and @ChanduMohammad , I am executing this script outside A360 and getting response but while executing same in A360 it is saying Bot error. Can you please check and let me know where exactly the issue is? do I need to install any libraries, if yes please guide me through. thank you.

import datetime
import base64
import hmac
import hashlib
import time
 
 
def hmac_sha256(message, key):
    key = bytes(key, 'UTF-8')
    message = bytes(message, 'UTF-8')
    h = hmac.new(key, message, hashlib.sha256)
    return base64.b64encode(h.digest()).decode()
 
def authorization(strAKID, strAKSecret):
    #AK/SK information
    strDateNow = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
    print(f"date is: {strDateNow}")
    strSignContent = "date: {}".format(strDateNow)
    #Compute signature, get authorization
    strSignature = hmac_sha256(strSignContent, strAKSecret)
    print(f"signature is: {strSignature}")
    strAuthorization = '''hmac username="{}", algorithm="hmac-sha256", headers="date", signature="{}"'''.format(strAKID, strSignature)
if __name__ == '__main__':
    # miral test env 
    strAKID = "e2c5b464-5361-40f2-9c8e-bc04962db1a7"
    strAKSecret = "I5lGga3vfFW8Nf9HbRHuUnROcga0SnpP"
    authorization(strAKID, strAKSecret)

Reply