Skip to main content
Navigator | Tier 3
January 30, 2022
Solved

python function call giving bot error

  • January 30, 2022
  • 6 replies
  • 365 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

    Best answer by Ashwin A.K

    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

    6 replies

    Most Valuable Pathfinder
    January 30, 2022

    Hi @mark fung-a-fat​ ,

     

    Can you share your AA Code Snippet?

    Chand
    mark 4254Author
    Navigator | Tier 3
    January 30, 2022

    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

    Ashwin A.K
    Navigator | Tier 3
    January 30, 2022

    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

    Blogs at TheCodingTheory - https://www.thecodingtheory.com
    mark 4254Author
    Navigator | Tier 3
    January 30, 2022
    ashwin this worked thank you.
    Ashwin A.K
    Navigator | Tier 3
    January 30, 2022

    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

    Blogs at TheCodingTheory - https://www.thecodingtheory.com
    Cadet | Tier 2
    April 16, 2024

    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)