Skip to content

Commit 9c87aa3

Browse files
committed
Adding getSessionId and onPostActivity
1 parent c7b8af7 commit 9c87aa3

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

src/directLine.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { AjaxResponse, AjaxRequest } from 'rxjs/observable/dom/AjaxObservable';
44
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
5+
import { Subject } from 'rxjs/Subject';
56
import { Observable } from 'rxjs/Observable';
67
import { Subscriber } from 'rxjs/Subscriber';
78
import { Subscription } from 'rxjs/Subscription';
@@ -102,7 +103,7 @@ export interface OAuth {
102103
contentType: "application/vnd.microsoft.card.oauth",
103104
content: {
104105
text?: string,
105-
name: string,
106+
connectionname: string,
106107
buttons?: CardAction[]
107108
}
108109
}
@@ -286,12 +287,16 @@ export interface IBotConnection {
286287
activity$: Observable<Activity>,
287288
end(): void,
288289
referenceGrammarId?: string,
289-
postActivity(activity: Activity): Observable<string>
290+
postActivity(activity: Activity): Observable<string>,
291+
getSessionId(): Observable<string>,
292+
onPostActivity: Observable<Activity>
290293
}
291294

292295
export class DirectLine implements IBotConnection {
293296
public connectionStatus$ = new BehaviorSubject(ConnectionStatus.Uninitialized);
294297
public activity$: Observable<Activity>;
298+
public onPostActivity: Observable<Activity>;
299+
private onPostActivitySubject: Subject<Activity>;
295300

296301
private domain = "https://directline.botframework.com/v3/directline";
297302
private webSocket;
@@ -312,6 +317,9 @@ export class DirectLine implements IBotConnection {
312317
this.token = options.secret || options.token;
313318
this.webSocket = (options.webSocket === undefined ? true : options.webSocket) && typeof WebSocket !== 'undefined' && WebSocket !== undefined;
314319

320+
this.onPostActivitySubject = new Subject<Activity>();
321+
this.onPostActivity = this.onPostActivitySubject.asObservable();
322+
315323
if (options.domain)
316324
this.domain = options.domain;
317325
if (options.conversationId) {
@@ -474,8 +482,35 @@ export class DirectLine implements IBotConnection {
474482
this.tokenRefreshSubscription.unsubscribe();
475483
this.connectionStatus$.next(ConnectionStatus.Ended);
476484
}
485+
486+
getSessionId(): Observable<string> {
487+
// If we're not connected to the bot, get connected
488+
// Will throw an error if we are not connected
489+
konsole.log("getSessionId");
490+
return this.checkConnection(true)
491+
.flatMap(_ =>
492+
Observable.ajax({
493+
method: "GET",
494+
url: `${this.domain}/session/getsessionid`,
495+
withCredentials: true,
496+
timeout,
497+
headers: {
498+
"Content-Type": "application/json",
499+
"Authorization": `Bearer ${this.token}`
500+
}
501+
})
502+
.map(ajaxResponse => {
503+
konsole.log("getSessionId response: " + ajaxResponse.response.sessionId);
504+
return ajaxResponse.response.sessionId as string;
505+
})
506+
.catch(error => this.catchPostError(error))
507+
)
508+
.catch(error => this.catchExpiredToken(error));
509+
}
477510

478511
postActivity(activity: Activity) {
512+
this.onPostActivitySubject.next(activity);
513+
479514
// Use postMessageWithAttachments for messages with attachments that are local files (e.g. an image to upload)
480515
// Technically we could use it for *all* activities, but postActivity is much lighter weight
481516
// So, since WebChat is partially a reference implementation of Direct Line, we implement both.

0 commit comments

Comments
 (0)