When looking for long-polling in the Skype APIA year ago, Microsoft
introduced a platform for creating bots for Skype. The platform provides a convenient message format, you can send cards with buttons, as in a telegram, in a word, everything looks very cool.
Recently, I needed to write a bot for Skype. And despite the fact that the topic was raised on a habr (
for example ), I faced some difficulties, I really did not have enough step-by-step guide on working with REST API.
')
In fact, the bot for skype is written quite quickly, if you know about the pitfalls and know
where to watch the documentation. The main idea I had to learn was: No web polling. If Telegram is provided by a long-polling and webbooks, then Skype can be managed only by webhacks. From this, the following problems arise - communication with Skype servers occurs only via https, and only with a valid certificate. We'll have to find the domain name, hosting, bother with the certificate.
How to do it?
So, from the very beginning. Suppose that we have a bare hosting (I had this Raspberry Pi, on which I tested the bot, but it could be a server on Amazon, for example). First, you need to get a domain name and a certificate for it. Free domain names can be obtained for example
here . Next we need to get a certificate for this name. This is easiest to do with
Let's Encrypt . During the certificate installation process, select the option standalone server - certbot will launch its server to verify that you own this domain name before issuing a certificate for it. After you have succeeded, you must have certificates in the “/ etc / letsencrypt / archive” folder.
Now that everything is ready for work, let's start writing a bot.
1. Much is described in
this article , we need two points: Here
we create our application, and
here we register the bot. When registering, you need to specify the endpoint, where Microsoft will send messages. It is specified in the same form as it is written in the browser. After endpoint, you can specify a port, then it will look like this: “https: // endpoint_url: port” After registration, you will need to generate a password, and that's all we needed to get at the moment. At the moment we should have: application_id (with which we registered the bot) and password (which we just received).
2. You have not forgotten about the webcam? It is time to raise the server for which we previously received certificates. On the python for this fitsk. First of all, let's define global variables (not a very nice solution, but very simple), and start automatically receiving and updating the token in a separate thread:
View codeFLASK = Flask(__name__) APP_ID = '' PASSWORD = ''
3. Let's write a handler function for incoming messages:
View code @FLASK.route('/', methods=['GET', 'POST']) def handle(): data = request.get_json() talk_id = data['conversation']['id'] msg = { "type": "message", "from": { "id": APP_ID, "name": "habraechobot" }, "conversation": { "id": talk_id, }, "text": data['text'], } url = data['serviceUrl'] + '/v3/conversations/{}/activities/'.format(data['conversation']['id']) headers = {'Authorization': 'Bearer ' + TOKEN['access_token'], 'content-type': 'application/json; charset=utf8'} r = requests.post(url, headers=headers, data=json.dumps(msg)) return 'success'
4. And finally, let's start the server:
View code if __name__ == '__main__': thread = Thread( target=get_and_verify_token ) thread.start() FLASK.run(host='0.0.0.0', port=8080, ssl_context=context)
That's all - the full code of the bot can be taken
from here , I hope my article was useful and will save someone time and effort.