Allows queries to be performed over AX25 packet.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.9 KiB

  1. #!/usr/bin/python3
  2. import wolframalpha
  3. import sys, fcntl, os
  4. #Change these
  5. wa_appid = "" #Wolfram app id
  6. EOL = "\r"
  7. pkt_len = 200
  8. wa_client = None
  9. connected = True
  10. def chunkstring(string, length):
  11. return (string[0+i:length+i] for i in range(0, len(string), length))
  12. def print_ax(str): #Formats for ax25d
  13. str += EOL
  14. for chunk in chunkstring(str, pkt_len):
  15. sys.stdout.write(chunk)
  16. sys.stdout.flush()
  17. def help():
  18. print_ax("Available commands:")
  19. print_ax("ask <question>: Asks wolfram alpha (Uses internet. Do not rely on this in an emergency!)")
  20. print_ax(" EX: ask Who is the prime minister of Canada")
  21. print_ax("help: Display this message")
  22. print_ax("bye: Disconnect from the host")
  23. def ask(query): #Wolfram alpha
  24. global wa_client
  25. global wa_appid
  26. if wa_client is None:
  27. wa_client = wolframalpha.Client(wa_appid)
  28. res = wa_client.query(query)
  29. sys.stdout.write(next(res.results).text + EOL)
  30. sys.stdout.flush()
  31. #Set input to be non-blocking
  32. fd = sys.stdin.fileno()
  33. fl = fcntl.fcntl(fd, fcntl.F_GETFL)
  34. fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
  35. readbuf = ""
  36. #Greet user
  37. print_ax("Welcome to the VA1QLE packet radio assistant v0.1. You can query Wolfram Alpha and Wikipedia here.")
  38. #Show commands
  39. help()
  40. while connected:
  41. readbuf += sys.stdin.read(4096)
  42. splitInput = readbuf.split(EOL)
  43. if(len(splitInput) > 1): #Stuff to process
  44. for text in splitInput[:-1]: #There may be multiple commands
  45. splitted = text.split(' ', 1)
  46. if splitted[0] == 'help':
  47. help()
  48. elif splitted[0] == 'ask':
  49. try:
  50. ask(splitted[1])
  51. except:
  52. print_ax("Error connecting to wolfram alpha")
  53. elif splitted[0] == 'bye':
  54. print_ax("73 and Goodbye.")
  55. connected = False
  56. elif splitted[0] != '':
  57. print_ax("ERROR: Command not found. Use 'help' to list available commands.")
  58. readbuf = splitInput[-1] #Take what's left and put it back in the buffer