Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

app.js 2.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  1. // This loads the environment variables from the .env file
  2. require('dotenv-extended').load();
  3. var builder = require('botbuilder');
  4. var restify = require('restify');
  5. var Promise = require('bluebird');
  6. var request = require('request-promise').defaults({ encoding: null });
  7. // Setup Restify Server
  8. var server = restify.createServer();
  9. server.listen(process.env.port || process.env.PORT || 3978, function () {
  10. console.log('%s listening to %s', server.name, server.url);
  11. });
  12. // Create chat bot
  13. var connector = new builder.ChatConnector({
  14. appId: process.env.MICROSOFT_APP_ID,
  15. appPassword: process.env.MICROSOFT_APP_PASSWORD
  16. });
  17. // Listen for messages
  18. server.post('/api/messages', connector.listen());
  19. var bot = new builder.UniversalBot(connector, function (session) {
  20. var msg = session.message;
  21. if (msg.attachments.length) {
  22. // Message with attachment, proceed to download it.
  23. // Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
  24. var attachment = msg.attachments[0];
  25. var fileDownload = checkRequiresToken(msg)
  26. ? requestWithToken(attachment.contentUrl)
  27. : request(attachment.contentUrl);
  28. fileDownload.then(
  29. function (response) {
  30. // Send reply with attachment type & size
  31. var reply = new builder.Message(session)
  32. .text('Attachment of %s type and size of %s bytes received.', attachment.contentType, response.length);
  33. session.send(reply);
  34. }).catch(function (err) {
  35. console.log('Error downloading attachment:', { statusCode: err.statusCode, message: err.response.statusMessage });
  36. });
  37. } else {
  38. // No attachments were sent
  39. var reply = new builder.Message(session)
  40. .text('Hi there! This sample is intented to show how can I receive attachments but no attachment was sent to me. Please try again sending a new message with an attachment.');
  41. session.send(reply);
  42. }
  43. });
  44. // Request file with Authentication Header
  45. var requestWithToken = function (url) {
  46. return obtainToken().then(function (token) {
  47. return request({
  48. url: url,
  49. headers: {
  50. 'Authorization': 'Bearer ' + token,
  51. 'Content-Type': 'application/octet-stream'
  52. }
  53. });
  54. });
  55. };
  56. // Promise for obtaining JWT Token (requested once)
  57. var obtainToken = Promise.promisify(connector.getAccessToken.bind(connector));
  58. var checkRequiresToken = function (message) {
  59. return message.source === 'skype' || message.source === 'msteams';
  60. };
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...