Coder Social home page Coder Social logo

abhishekchikara / example.messengerchatbot.python Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lseg-api-samples/example.messengerchatbot.python

0.0 1.0 0.0 252 KB

The Messenger Bot API example/demo source code in Python language.

Home Page: https://developers.refinitiv.com/messenger-api

License: Other

Python 100.00%

example.messengerchatbot.python's Introduction

Messenger Bot API Demo Applications with Python

Messenger Bot API Introduction

Refinitiv's Messenger application is a free to use, compliant and secure messaging platform. It is a powerful communication tool that provides desktop, mobile, and web access, and allows sharing messages, data, files, screenshots, and emoticons with your contacts.

The Messenger Bot API provides a set of available API calls to build automated workflows or bots for the Messenger application. The Bot API allows your applications to connect with and pass information into Eikon’s Messenger service programmatically or interact with a bot via a WebSocket connection.

Table of contents

Messenger Bot API Demo overview

This demo project shows how to implement the Messenger BOT API console applications with the REST and WebSocket APIs in Python programming language. The applications makes an authentication with RDP Authentication service, then lets developers interact with Messeger Application/Chatroom on behalf of Bot via the REST and WebSocket APIs.

  • chatbot_demo_rest.py: A REST API example that sends chat message to individual and a Chatroom.
  • chatbot_demo_ws.py: A REST and WebSocket APIs example that sends and receives chat message with a Chatroom.
  • rdp_token.py: A Python module that manages RDP Authentication process for chatbot_demo_rest.py and chatbot_demo_ws.py applications. This module is based on RDP Python Quickstart Python source code implemented by Gurpreet Bal.

Note: Please note that the main concept for using Messager Bot REST and WebSocket APIs are the same for all technologies (see JavaScript example).

Prerequisite

This tutorial source code requires the following dependencies.

  1. Messenger application.
  2. Python compiler and runtime
  3. Python's requests 2.x library for both REST and WebSocket connections.
  4. Python's websocket-client library (version 0.49 or greater) for the WebSocket connection .
  5. Messenger Bot API access and license.

Please contact your Refinitiv's representative and Dino Diviacchi ([email protected]) to help you to access Messenger application and Bot API. The Refinitiv team will then provision and set up the bot. Once this is done the email user you provided will receive an automated email with how to set up a password for the bot.

Note:

  • The Python example has been qualified with Python versions 3.6.5
  • Please refer to the pip installation guide page if your environment does not have the pip tool installed.
  • If your environment already have a websocket-client library installed, you can use pip list command to verify a library version, then use pip install --upgrade websocket-client command to upgrade websocket-client library.

Getting the AppKey value

Once you have setup your Messenger user, please access the AppKey Generator Tool via Eikon Desktop/Refinitiv Workspace application (go to the Search Bar and type APP KEY, then select the AppKey Generator) or via a web site (Please access with your Eikon account, not your bot account).

Figure-1

You can generate your AppKey via the following steps:

  1. Enter an App Display Name
  2. Select the tick box next to EDP API as the API Type
  3. Click ‘Register’ New App button

You will then see a row for your new app with an AppKey item, which is your client_id for the Refinitiv Data Platform (RDP) Authentication.

Setting the Messenger application

Once you have setup your Messenger user and Bot user, you can add the Bot and create a Chatroom for you and your Bot via the following steps

  1. Login to your personal Messenger to add the bot to your contacts, using “Add a New Contact” from the menu in the lower left corner.

    Figure-2

  2. Add bot name [email protected].

    Figure-3

  3. Once you have add the bot it will show up under your contacts (on the left side).

    Figure-4

  4. Create a chatroom using "Create a Bilateral chatroom" button from the menu in the lower left corner.

    Figure-5

  5. Add your Bot to a chatroom by dragging it into your newly created chatroom.

    Figure-6

Running the REST API demo application

  1. Unzip or download the tutorial project folder into a directory of your choice
  2. Open a command prompt or console.
  3. Run $>pip install -r rest-requirements.txt command in a console to install all the dependencies libraries.
  4. Open the chatbot_demo_rest.py demo application source code with your editor and input the following parameters
    • app_key: Your AppKey
    • bot_username: Your Bot username
    • bot_password: Your Bot password
    • recipient_email : Your assoicate Messenger email address
  5. OOpen a command prompt and go to folder src, then run the demo application with the following command.
    $>python chatbot_demo_rest.py
    
  6. The application then authenticates with RDP Authentication service and sends a 1-1 message to your assoicate Eikon message email address.
    Getting RDP Authentication Token
    
    Saving Authentication information to file
    Successfully Authenticated
    send 1 to 1 message to <email>
    Messenger BOT API: post a 1 to 1 message to <email> success
    
  7. Then a demo gets an associate Chatroom and joining to that Chatroom.
    Get Rooms
    Messenger BOT API: get chatroom  success
    Join Rooms
    Messenger BOT API: join chatroom success
    
  8. The demo application will send a default Hello from Python message to a Chatroom on behalf of the Bot API and lets you send your own message before leaving a Chatroom.
    sending message to {...} Rooms
    Messenger BOT API: post message to chatroom success
    Please input your message: Wendy, I'm home.
    Messenger BOT API: post message to chatroom success
    Leave Rooms
    Messenger BOT API: leave chatroom success
    

Running the WebSocket API demo application

  1. Unzip or download the tutorial project folder into a directory of your choice.
  2. Open a command prompt or console.
  3. Run $>pip install -r ws-requirements.txt command in a console to install all the dependencies libraries.
  4. Open the chatbot_demo_ws.py demo application source code with your editor and input the following parameters
    • app_key: Your AppKey
    • bot_username: Your Bot username
    • bot_password: Your Bot password
    • recipient_email : Your assoicate Messenger email address (For 1 to 1 message)
  5. Open a command prompt and go to folder src, then run the demo application with the following command.
    $>python chatbot_demo_ws.py
    
  6. The demo will perform authentication process, get an assoicate chatroom, then join that chatroom as same as the REST API demo application above
  7. Then demo connects to the Messenger BOT API WebSocket server. Once the application shows WebSocket connected event in a console, you can start interact with your bot via a chatroom.
    Messenger BOT API: join chatroom success
    Connecting to WebSocket wss://api.collab.refinitiv.com/services/nt/api/messenger/v1/stream ...
    WebSocket Connection is established
    Sent:....
    Received:
    {
    "event":"connected",
    "reqId":"943378"
    }
    
  8. Now we should be able to receive a response from our chatbot. You can test by sending hello message in to a Chatroom to see an example.
  9. The Messenger application supports tabular data, hyperlinks and a full set of emoji in the message. You can type /complex_message message into a Chatroom to see an example. Figure-7

Running demo applications with debug log

You can enable the REST and WebSocket application debug log level via log_level = logging.WARN application source code statement.The supported values are logging.WARN and logging.DEBUG levels.

The logging.DEBUG level shows incoming and outgoing messages between the demo applications and Messenger BOT API REST and WebSocket servers.

Authors

References

For further details, please check out the following resources:

example.messengerchatbot.python's People

Contributors

wasin-waeosri avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.