The Python client is built in an event handler architecture. If you are building a bot that listens to conversations, you will only have to implement an interface of a listener with the functions to handle all events that will come through the Data Feed.
- Add the Configuration file as a resource of your project
- Create a main class that includes the initialization of the client and load the config by pointing to the path to your config.json.
- Implement the RoomListener, IMListener, and/or ConnectionListener classes and add them to the DatafeedEventsService, which reads events on the data feed and triggers your implemented functions.
Symphony Generator Available for Python
The Symphony Generator includes an example of a simple test bot that has all the necessary moving parts to get you started in minutes.
- Follow the instructions to install and run the Symphony Generator
- Follow the prompts to get a Python bot
- Open the project in your IDE of choice. Upon opening the project, navigate to the top level directory and run
pip install -r requirements.txtin your command line. This will go ahead and grab all the necessary dependencies for your bot to run. Make sure that the requirements.txt has the latest version of
sym-api-client-pythonpip package by checking https://pypi.org/project/sym-api-client-python/.
Python >= 3.6
- After installing the latest version of pip package, check to make sure the config.json file was generated correctly. This includes populating any additional fields such as
proxyPortif needed. It is important to note that the generator setup such that the developer has the ability to authenticate using certificates or RSA. If you are using certificates, make sure that config.json is the configuration file you are working with. If you are using RSA authentication, use configRSA.json.
- You are now ready to start your symphony bot. Navigate to your main.py or mainRSA.py file depending on your authentication approach check that the path to your config file is correctly passed into the SymConfig object.
# pip install -r requirements.txt #sudo npm link to test generator locally from sym_api_client_python.configure.configure import SymConfig from sym_api_client_python.auth.auth import Auth from sym_api_client_python.clients.SymBotClient import SymBotClient from roomListenerImp import RoomListenerTestImp from IMListenerImp import IMListenerTestImp #debug logging --> set to debug --> check logs/example.log import logging logging.basicConfig(filename='example.log', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', filemode='w', level=logging.DEBUG) logging.getLogger("urllib3").setLevel(logging.WARNING) #main() acts as executable script --> run python3 hello.py to start Bot... def main(): print('hi') #RSA Auth flow: #certificate Auth flow: configure = SymConfig('../resources/config.json') configure.loadFromFile() auth = Auth(configure) auth.authenticate() #initialize SymBotClient with auth and configure objects botClient = SymBotClient(auth, configure) #initialize datafeed service DataFeedEventService = botClient.getDataFeedEventService() #initialize listener classes and append them to DataFeedEventService #class #these listener classes sit in DataFeedEventService class as a way to #easily handle events coming back from the DataFeed imListenerTest = IMListenerTestImp(botClient) DataFeedEventService.addIMListener(imListenerTest) roomListenerTest = RoomListenerTestImp(botClient) DataFeedEventService.addRoomListener(roomListenerTest) #create data feed and read datafeed recursively print('starting datafeed') DataFeedEventService.startDataFeed() if __name__ == "__main__": main()
Example roomListener Class below:
from sym_api_client_python.listeners.RoomListener import RoomListener import calendar; import time; #sample implementation of Abstract RoomListener class #has instance of SymBotClient so that it can respond to events coming in by leveraging other clients on SymBotClient #each function should contain logic for each corresponding event class RoomListenerTestImp(RoomListener): def __init__(self, SymBotClient): self.botClient = SymBotClient def onRoomMessage(self, message): logging.debug('room message recieved', message) #sample code for developer to implement --> use MessageClient and #data recieved from message event to reply with a #reed streamId = message['stream']['streamId'] message = dict(message = '<messageML><hash tag="ReedF"/></messageML>') self.botClient.getMessageClient().sendMessage(streamId, message) def onRoomCreated(self, roomCreated): logging.debug('room created', roomCreated) def onRoomDeactivated(self, roomDeactivated): logging.debug('room Deactivated', roomDeactivated) def onRoomMemberDemotedFromOwner(self, roomMemberDemotedFromOwner): logging.debug('room member demoted from owner', roomMemberDemotedFromOwner) def onRoomMemberPromotedToOwner(self, roomMemberPromotedToOwner): logging.debug('room member promoted to owner', roomMemberPromotedToOwner) def onRoomReactivated(self, roomReactivated): logging.debug('room reactivated', roomReactivated) def onRoomUpdated(self, roomUpdated): logging.debug('room updated', roomUpdated) def onUserJoinedRoom(self, userJoinedRoom): logging.debug('USER JOINED ROOM', userJoinedRoom) def onUserLeftRoom(self, userLeftRoom): logging.debug('USER LEFT ROOM', userLeftRoom)
- To start bot, enter
python3 main.pyin your command line. To show to capabilities of the sample
rclass code above, create a chat room in your symphony pod and add your bot to the room. If you type in the room, the bot will respond with a #reedbot as seen programmed in the
Updated 9 months ago