PySkype

Unofficial library for Skype API

View project on GitHub

To develop you own Skype bot, you need to create bot on Microsoft Bots Framework. So, here is the guidline, step-by-step:

  1. Open up this page. Sign in into you personal Microsoft account or create it. Than you'll see something, like this:
  2. On the opened page press "Create a bot" and than "Register"
  3. Enter data into required fields and generate Bot app ID and password, by clicking on "Create Microsoft App ID and password (REMEMBER: You have to save password, which was generated)
  4. After that, click "Register". If everithing correct, you'll see tool tip, which infrom you, that bot was successfully created and you'll be redirected to bot setting page:

Now, you need to create backend for you bot. For this example we’ll use Flask + PySkype.

  1. Start the new project. Create virtualenvironment and activate it:
          $ virtualenv -p python3.5 venv
          $ source venv/bin/activate
        
  2. Install Flask, PySkype('requests' must be installed automatically with PySkype):
          $ pip install flask
          $ pip install PySkype
        
  3. Create base file for Flask application:
          from flask import Flask, request
          BOT_HOST = '127.0.0.1'
          BOT_PORT = 8080
          app = Flask('WorkBot')
          @app.route('/')
          def hello():
            return "Hello World!"
          if __name__ == '__main__':
            app.run(host=BOT_HOST, port=BOT_PORT, debug=True)
        
    And run app, to test, is everithing works well.
  4. From PySkype import SkypeBot. Create an object and pass client id, password from Bot Framework and path to current directory, as parameters:
          CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
          CLIENT_ID = 'You loong id from Bot Framework, that you created previously'
          CLIENT_PASSWORD = 'Super secret password, that you created previously'
          bot = SkypeBot(CLIENT_ID, CLIENT_PASSWORD, CURRENT_DIR)
        
  5. Also add endpoint to receiving messages from Microsoft:
          @app.route('/api/messages', methods=['POST'])
          def messages():
            if request.method == 'POST':
              print('post request received')
            else:
              print('not post request')
            return 'Bot is working'
        
  6. Now time to use ngrok to create HTTPS tonnel for receiving messages from Microsoft. Download it, unzip and run:
    $ ./ngrok http 8080
    Select https url.
  7. Open up list of you bots, select you bot and swich to 'SETTINGS':
  8. Paste you ngrok https url to 'Messaging endpoint' input (with you Flask endpoint) and press 'Save changes':
  9. Get URL for adding you bot to the contacts. Switch to 'CHANNELS' and tap on lable 'Skype'. Click 'Add to Contacts' and you bot will be added to your contacts. *NOTICE: for users, that used OS Linux I reccomend copy this URL and add you bot on your smartphone, because Skype for Linux now not supporting adding bots.
  10. Run you app and send something to you bot. If everithing OK, you'll see request in the console and on ngrok window
  11. For receiving some messages from you bot, you need to get conversation id and service url from received request and use send_message method of bot object:
          data = json.loads(request.data.decode('utf8'))
          sender = data['conversation']['id']
          service = data['serviceUrl']
          bot.send_message(service, sender, "YEAH, I'm working!")
        
  12. And.. that's all=) All sources you can find by following URL. To get details power of PySkype, read README. And good luck!