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 4.3 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  1. /*-----------------------------------------------------------------------------
  2. A speech to text bot for the Microsoft Bot Framework.
  3. -----------------------------------------------------------------------------*/
  4. // This loads the environment variables from the .env file
  5. require('dotenv-extended').load();
  6. var builder = require('botbuilder'),
  7. fs = require('fs'),
  8. needle = require('needle'),
  9. restify = require('restify'),
  10. request = require('request'),
  11. url = require('url'),
  12. speechService = require('./speech-service.js');
  13. //=========================================================
  14. // Bot Setup
  15. //=========================================================
  16. // Setup Restify Server
  17. var server = restify.createServer();
  18. server.listen(process.env.port || process.env.PORT || 3978, function () {
  19. console.log('%s listening to %s', server.name, server.url);
  20. });
  21. // Create chat bot
  22. var connector = new builder.ChatConnector({
  23. appId: process.env.MICROSOFT_APP_ID,
  24. appPassword: process.env.MICROSOFT_APP_PASSWORD
  25. });
  26. server.post('/api/messages', connector.listen());
  27. var bot = new builder.UniversalBot(connector, function (session) {
  28. if (hasAudioAttachment(session)) {
  29. var stream = getAudioStreamFromMessage(session.message);
  30. speechService.getTextFromAudioStream(stream)
  31. .then(function (text) {
  32. session.send(processText(text));
  33. })
  34. .catch(function (error) {
  35. session.send('Oops! Something went wrong. Try again later.');
  36. console.error(error);
  37. });
  38. } else {
  39. session.send('Did you upload an audio file? I\'m more of an audible person. Try sending me a wav file');
  40. }
  41. });
  42. //=========================================================
  43. // Utilities
  44. //=========================================================
  45. function hasAudioAttachment(session) {
  46. return session.message.attachments.length > 0 &&
  47. (session.message.attachments[0].contentType === 'audio/wav' ||
  48. session.message.attachments[0].contentType === 'application/octet-stream');
  49. }
  50. function getAudioStreamFromMessage(message) {
  51. var headers = {};
  52. var attachment = message.attachments[0];
  53. if (checkRequiresToken(message)) {
  54. // The Skype attachment URLs are secured by JwtToken,
  55. // you should set the JwtToken of your bot as the authorization header for the GET request your bot initiates to fetch the image.
  56. // https://github.com/Microsoft/BotBuilder/issues/662
  57. connector.getAccessToken(function (error, token) {
  58. var tok = token;
  59. headers['Authorization'] = 'Bearer ' + token;
  60. headers['Content-Type'] = 'application/octet-stream';
  61. return needle.get(attachment.contentUrl, { headers: headers });
  62. });
  63. }
  64. headers['Content-Type'] = attachment.contentType;
  65. return needle.get(attachment.contentUrl, { headers: headers });
  66. }
  67. function checkRequiresToken(message) {
  68. return message.source === 'skype' || message.source === 'msteams';
  69. }
  70. function processText(text) {
  71. var result = 'You said: ' + text + '.';
  72. if (text && text.length > 0) {
  73. var wordCount = text.split(' ').filter(function (x) { return x; }).length;
  74. result += '\n\nWord Count: ' + wordCount;
  75. var characterCount = text.replace(/ /g, '').length;
  76. result += '\n\nCharacter Count: ' + characterCount;
  77. var spaceCount = text.split(' ').length - 1;
  78. result += '\n\nSpace Count: ' + spaceCount;
  79. var m = text.match(/[aeiou]/gi);
  80. var vowelCount = m === null ? 0 : m.length;
  81. result += '\n\nVowel Count: ' + vowelCount;
  82. }
  83. return result;
  84. }
  85. //=========================================================
  86. // Bots Events
  87. //=========================================================
  88. // Sends greeting message when the bot is first added to a conversation
  89. bot.on('conversationUpdate', function (message) {
  90. if (message.membersAdded) {
  91. message.membersAdded.forEach(function (identity) {
  92. if (identity.id === message.address.bot.id) {
  93. var reply = new builder.Message()
  94. .address(message.address)
  95. .text('Hi! I am SpeechToText Bot. I can understand the content of any audio and convert it to text. Try sending me a wav file.');
  96. bot.send(reply);
  97. }
  98. });
  99. }
  100. });
Tip!

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

Comments

Loading...