Skip to content

Instantly share code, notes, and snippets.

@qxf2
Created Aug 27, 2020
Embed
What would you like to do?
How Qxf2 Skype sender endpoint has been setup
"""
Qxf2's Skype sender
"""
from flask import Flask, jsonify, request
from skpy import Skype, SkypeAuthException, SkypeConnection
import skype_credentials as creds
app = Flask(__name__)
def login(username, password, token_file='.tokens-app'):
"Login to Skype"
sk = Skype(connect=False)
sk.conn.setTokenFile(token_file)
try:
sk.conn.readToken()
except SkypeAuthException:
sk.conn.setUserPwd(username, password)
sk.conn.getSkypeToken()
return sk
def post_message(sk, channel_id, msg):
"Post a message to a given channel"
result_flag = False
try:
sk.conn.verifyToken(SkypeConnection.Auth.SkypeToken)
channel = sk.chats.chat(channel_id)
channel.sendMsg(msg, rich=True)
result_flag = True
except Exception as e:
print(e)
return result_flag
@app.route('/send-message', methods=['POST'])
def send_message_endpoint():
"Send the required message"
request_data = request.get_json()
api_key = request_data.get('API_KEY', '')
channel_id = request_data.get('channel', None)
msg = request_data.get('msg', None)
if api_key != creds.API_KEY:
return jsonify({'msg':'incorrect API key'}), 401
if not msg or not channel_id:
return jsonify({'msg':'missing request parameters'}), 400
sk = login(creds.USERNAME, creds.PASSWORD)
post_message(sk, channel_id, msg)
return jsonify({'msg':'Posted message'}), 200
@app.route("/")
def index():
"Index page"
return jsonify({'msg':'this works'}), 200
#----START OF SCRIPT
if __name__=='__main__':
app.run('0.0.0.0', debug=True)
USERNAME = 'SKYPE ID'
PASSWORD = 'YOUR PASSWORD'
API_KEY = 'SOMETHING_YOU_SET'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment