Giter Site home page Giter Site logo

denissa4 / skype_chatbot Goto Github PK

View Code? Open in Web Editor NEW
18.0 4.0 10.0 15 KB

Simple script for Skype chatbot

Home Page: https://www.nlsql.com

License: MIT License

Python 100.00%
skype skype-for-business skype-bot skype-api nlsql skypebotpython pythonbot python-bot

skype_chatbot's Introduction

skype_chatbot

Python Skype Bot API for developing bots.

Recommendation:

Read through this guide before beginning bot development Register an application in Azure AD, Messages and activities

Overview:

  1. This API is tested with Python 3.6.
  2. Take app_id and app_secret from the app you will create on Register a Bot
  3. For sending and receiving requests will using Flask application(A template main.py is provided. You can use one as your bot web-page).

Simple Echo Bot

import skype_chatbot
import json
from flask import Flask, request 

app = Flask(__name__)

app_id = 'example_app_id'
app_secret = 'example_app_secret'

bot = skype_chatbot.SkypeBot(app_id, app_secret)

@app.route('/api/messages', methods=['POST', 'GET'])
def webhook():
  if request.method == 'POST':
    try:
        data = json.loads(request.data) 
        bot_id = data['recipient']['id']
        bot_name = data['recipient']['name']
        recipient = data['from']
        service = data['serviceUrl']
        sender = data['conversation']['id']
        text = data['text']

        bot.send_message(bot_id, bot_name, recipient, service, sender, text)
        
    except Exception as e:
      print(e)

  return 'Code: 200'
  
if __name__ == '__main__':

    context = ('domain.cer', 'domain.key')

    app.run(host='0.0.0.0', port=8000, debug=False, ssl_context=context)

How to use

Install package

Run in console: pip install skype_chatbot
or download package from GitHub and copy folder skype_chatbot to site-packages (e.g. C:\Python36\Lib\site-packages\skype_chatbot)

Create <main.py> file

Import skype_chatbot package:

import skype_chatbot

During installation input you app ID and secret key, once prompted:

app_id = "example_app_id"
app_secret = "example_app_secret"

Create object bot: bot = skype_chatbot.SkypeBot(client_id, client_secret)

Now you can use base methods:

send_message
send_media
create_animation_card
create_card_attachment
create_card_image
create_button
send_card
create_card_adaptive
create_item_for_adaptive_card
create_action_for_adaptive_card

Send message:

send_message(bot_id, bot_name, recipient, service, sender, text, text_format)

bot_id - skype bot id, you can get it from request data['recipient']['id'].
bot_name - skype bot name, you can get it from request data['recipient']['name'].
recipient - user, to whom you are sending the message. You can get it from request data['from'].
service - service url, you can get it from request data['serviceUrl'].
sender - conversation id, you can get it from request data['conversation']['id'].
text - text what you want to send recipient. Must be a string.
text_format - supported values: "plain", "markdown", or "xml" (default: "markdown").

Send media files:

send_media(bot_id, bot_name, recipient, service, sender, message_type, url, attachment_name)

bot_id - skype bot id, you can get it from request data['recipient']['id'].
bot_name - skype bot name, you can get it from request data['recipient']['name'].
recipient - user, to whom you are sending the message. You can get it from request data['from'].
service - service url, you can get it from request data['serviceUrl'].
sender - conversation id, you can get it from request data['conversation']['id'].
message_type - type of your media file, e.g. "image/png".
url - open url for your media file.
attachment_name - name, which is displayed to recipient.

Create card that can play animated GIFs or short videos:

create_animation_card(card_type, url, images, title, subtitle, text, buttons, autoloop, autostart, shareable)

card_type - type of card attachment ("hero", "thumbnail", "receipt").
url - open url for your animation file.
images - list of images, in card attachment (to create image use method create_card_image). Must be a list.
title - title for your card. Must be a string.
subtitle - subtitle for your card. Must be a string.
text - text for your card. Must be a string.
buttons - list of buttons, in card attachment (to create button use method create_button). Must be a list.
autoloop - default: True.
autostart - default: True.
shareable - default: True.

Create card attachment("hero", "thumbnail", "receipt"):

create_card_attachment(card_type, title, subtitle, text, images, buttons)

card_type - type of card attachment ("hero", "thumbnail", "receipt").
title - title for your card. Must be a string.
subtitle - subtitle for your card. Must be a string.
text - text for your card. Must be a string.
images - list of images, in card attachment (to create image use method create_card_image). Must be a list.
buttons - list of buttons, in card attachment (to create button use method create_button). Must be a list.

Create image for card:

create_card_image(url, alt)

url - open url for your image.
alt - alternative text for image.

Create button(actions) for card:

create_button(button_type, title, value)

button_type - type of your button(e.g. "openUrl", "postBack").
title - name of button.
value - value of button(e.g. if button_type="openUrl", value="example.com").

Action Type Content of value property
openUrl URL to be opened in the built-in browser.
imBack Text of the message to send to the bot (from the user who clicked the button or tapped the card). This message (from user to bot) will be visible to all conversation participants via the client application that is hosting the conversation.
postBack Text of the message to send to the bot (from the user who clicked the button or tapped the card). Some client applications may display this text in the message feed, where it will be visible to all conversation participants.
call Destination for a call in following format: "tel:123123123123"
playAudio URL of audio to be played
playVideo URL of video to be played
showImage show image referenced by URL
downloadFile URL of file to be downloaded
signin URL of OAuth flow to be initiated

Send card attachment to recipient:

send_card(bot_id, bot_name, recipient, reply_to_id, service, sender, message_type, card_attachment, text)

bot_id - skype bot id, you can get it from request data['recipient']['id'].
bot_name - skype bot name, you can get it from request data['recipient']['name'].
reply_to_id - the message id you are replying to, you can get it from request data['id'].
recipient - user, to whom you are sending the message. You can get it from request data['from'].
service - service url, you can get it from request data['serviceUrl'].
sender - conversation id, you can get it from request data['conversation']['id'].
message_type - if you send more than one card, choose display way("carousel" or "list").
card_attachment - list of cards, in message (to create cards use method create_card_attachment). Must be a list.
text - text of your message.

skype_chatbot's People

Contributors

ghost8recon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

skype_chatbot's Issues

Issue executing main.py

Hi,

I am getting this issue while executing main.py.

  • Serving Flask app "skype_run" (lazy loading)
  • Environment: production
    WARNING: Do not use the development server in a production environment.
    Use a production WSGI server instead.
  • Debug mode: on
  • Restarting with stat
  • Debugger is active!
  • Debugger PIN: 321-558-307
    Exception in thread Thread-2:
    Traceback (most recent call last):
    File "D:\Users\vinayver\AppData\Local\Continuum\anaconda3\lib\threading.py", l
    ine 916, in _bootstrap_inner
    self.run()
    File "D:\Users\vinayver\AppData\Local\Continuum\anaconda3\lib\threading.py", l
    ine 864, in run
    self._target(*self._args, **self._kwargs)
    File "D:\Users\vinayver\AppData\Local\Continuum\anaconda3\lib\site-packages\we
    rkzeug\serving.py", line 774, in inner
    fd=fd)
    File "D:\Users\vinayver\AppData\Local\Continuum\anaconda3\lib\site-packages\we
    rkzeug\serving.py", line 660, in make_server
    passthrough_errors, ssl_context, fd=fd)
    File "D:\Users\vinayver\AppData\Local\Continuum\anaconda3\lib\site-packages\we
    rkzeug\serving.py", line 601, in init
    self.socket = ssl_context.wrap_socket(sock, server_side=True)
    File "D:\Users\vinayver\AppData\Local\Continuum\anaconda3\lib\site-packages\we
    rkzeug\serving.py", line 511, in wrap_socket
    ssl_version=self._protocol, **kwargs)
    File "D:\Users\vinayver\AppData\Local\Continuum\anaconda3\lib\ssl.py", line 11
    49, in wrap_socket
    ciphers=ciphers)
    File "D:\Users\vinayver\AppData\Local\Continuum\anaconda3\lib\ssl.py", line 74
    7, in init
    self._context.load_cert_chain(certfile, keyfile)
    FileNotFoundError: [Errno 2] No such file or directory

Having to sleep between initializing and message sending

    bot = skype_chatbot.SkypeBot(os.environ['app_id'], os.environ['app_secret'])
    time.sleep(0.25)
    < some message stuff here > 
    bot.send_message(bot_id, bot_name, recipient, service, sender, print_me)

Without the sleep it fails as get_token has not completed execution/returned to main thread / whatever

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.