Facebook
From Capacious Elephant, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 187
  1. #!/usr/bin/python3
  2.  
  3. # test.py
  4. #
  5. # Usage:
  6. # Install mesibo python package from https://github.com/mesibo/python
  7. # Create an application at https://mesibo.com/console
  8. # Create a user
  9. # Obtain the token and app id for the user and set it
  10. # Run the script python3 test.py
  11. # Send a message from console to the user. It should be received and printed by your script
  12. # Press Ctrl+Z to stop the script
  13. #
  14. # Refer to https://mesibo.com/documentation/tutorials/first-app/python/
  15.  
  16. MESIBO_AUTH_TOKEN = "xxxx"
  17. MESIBO_APP_ID = "xxxx"
  18.  
  19. from mesibo import Mesibo
  20. from mesibo import MesiboNotify
  21.  
  22. class MesiboListener(MesiboNotify):
  23.  
  24.     def on_connectionstatus(self, status):
  25.         """A status = 1 means the listener successfully connected to the Mesibo server
  26.        """
  27.         print(f"## Connection status: {status}")
  28.         return 0
  29.  
  30.     def on_message(self, msg_params, data, datalen):
  31.         """Invoked on receiving a new message or reading database messages
  32.        """
  33.         print(f"## Received message from {msg_params['peer']}: {data} of len {datalen}")
  34.         print(msg_params)
  35.         print("Message: ", data[: datalen].decode('utf-8'))
  36.         return 0
  37.  
  38.     def on_messagestatus(self, msg_params):
  39.         """Invoked when the status of an outgoing or sent message is changed. Statuses can be
  40.        sent, delivered, or read
  41.        """
  42.         #print(f"## Outgoing msg. To: {msg_params['peer']}, Status: {msg_params['status']}")
  43.         return 0
  44.  
  45. def send_text_message(to,message):
  46.         #api is the Mesibo Python API instance.
  47.         #Make sure the instance is initialised before you call API functions
  48.         p = {}
  49.         p['peer'] = to
  50.         data = message
  51.         pymesibo.send_message(p, pymesibo.random(),data)
  52.  
  53. #Initialisation code
  54.  
  55. #Create a Mesibo Instance
  56. pymesibo = Mesibo()
  57.  
  58. #Set Listener
  59. pymesibo.set_listener(MesiboListener)  
  60.  
  61. #Set your AUTH_TOKEN obtained from the Mesibo Console
  62. pymesibo.set_accesstoken(MESIBO_AUTH_TOKEN)
  63.  
  64. pymesibo.set_database("mesibo.db")
  65.  
  66. #Set APP_ID which you used to create AUTH_TOKEN
  67. pymesibo.set_appname(MESIBO_APP_ID)
  68.  
  69. #Start mesibo
  70. pymesibo.start()
  71.  
  72. pymesibo.wait()