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.9 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  1. var Swagger = require('swagger-client');
  2. var open = require('open');
  3. var rp = require('request-promise');
  4. // config items
  5. var pollInterval = 1000;
  6. var directLineSecret = 'DIRECTLINE_SECRET';
  7. var directLineClientName = 'DirectLineClient';
  8. var directLineSpecUrl = 'https://docs.botframework.com/en-us/restapi/directline3/swagger.json';
  9. var directLineClient = rp(directLineSpecUrl)
  10. .then(function (spec) {
  11. // client
  12. return new Swagger({
  13. spec: JSON.parse(spec.trim()),
  14. usePromise: true
  15. });
  16. })
  17. .then(function (client) {
  18. // add authorization header to client
  19. client.clientAuthorizations.add('AuthorizationBotConnector', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + directLineSecret, 'header'));
  20. return client;
  21. })
  22. .catch(function (err) {
  23. console.error('Error initializing DirectLine client', err);
  24. });
  25. // once the client is ready, create a new conversation
  26. directLineClient.then(function (client) {
  27. client.Conversations.Conversations_StartConversation() // create conversation
  28. .then(function (response) {
  29. return response.obj.conversationId;
  30. }) // obtain id
  31. .then(function (conversationId) {
  32. sendMessagesFromConsole(client, conversationId); // start watching console input for sending new messages to bot
  33. pollMessages(client, conversationId); // start polling messages from bot
  34. });
  35. });
  36. // Read from console (stdin) and send input to conversation using DirectLine client
  37. function sendMessagesFromConsole(client, conversationId) {
  38. var stdin = process.openStdin();
  39. process.stdout.write('Command> ');
  40. stdin.addListener('data', function (e) {
  41. var input = e.toString().trim();
  42. if (input) {
  43. // exit
  44. if (input.toLowerCase() === 'exit') {
  45. return process.exit();
  46. }
  47. // send message
  48. client.Conversations.Conversations_PostActivity(
  49. {
  50. conversationId: conversationId,
  51. activity: {
  52. textFormat: 'plain',
  53. text: input,
  54. type: 'message',
  55. from: {
  56. id: directLineClientName,
  57. name: directLineClientName
  58. }
  59. }
  60. }).catch(function (err) {
  61. console.error('Error sending message:', err);
  62. });
  63. process.stdout.write('Command> ');
  64. }
  65. });
  66. }
  67. // Poll Messages from conversation using DirectLine client
  68. function pollMessages(client, conversationId) {
  69. console.log('Starting polling message for conversationId: ' + conversationId);
  70. var watermark = null;
  71. setInterval(function () {
  72. client.Conversations.Conversations_GetActivities({ conversationId: conversationId, watermark: watermark })
  73. .then(function (response) {
  74. watermark = response.obj.watermark; // use watermark so subsequent requests skip old messages
  75. return response.obj.activities;
  76. })
  77. .then(printMessages);
  78. }, pollInterval);
  79. }
  80. // Helpers methods
  81. function printMessages(activities) {
  82. if (activities && activities.length) {
  83. // ignore own messages
  84. activities = activities.filter(function (m) { return m.from.id !== directLineClientName });
  85. if (activities.length) {
  86. process.stdout.clearLine();
  87. process.stdout.cursorTo(0);
  88. // print other messages
  89. activities.forEach(printMessage);
  90. process.stdout.write('Command> ');
  91. }
  92. }
  93. }
  94. function printMessage(activity) {
  95. if (activity.text) {
  96. console.log(activity.text);
  97. }
  98. if (activity.attachments) {
  99. activity.attachments.forEach(function (attachment) {
  100. switch (attachment.contentType) {
  101. case "application/vnd.microsoft.card.hero":
  102. renderHeroCard(attachment);
  103. break;
  104. case "image/png":
  105. console.log('Opening the requested image ' + attachment.contentUrl);
  106. open(attachment.contentUrl);
  107. break;
  108. }
  109. });
  110. }
  111. }
  112. function renderHeroCard(attachment) {
  113. var width = 70;
  114. var contentLine = function (content) {
  115. return ' '.repeat((width - content.length) / 2) +
  116. content +
  117. ' '.repeat((width - content.length) / 2);
  118. }
  119. console.log('/' + '*'.repeat(width + 1));
  120. console.log('*' + contentLine(attachment.content.title) + '*');
  121. console.log('*' + ' '.repeat(width) + '*');
  122. console.log('*' + contentLine(attachment.content.text) + '*');
  123. console.log('*'.repeat(width + 1) + '/');
  124. }
Tip!

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

Comments

Loading...