Skip to content

Commit

Permalink
Adding getSessionId and onPostActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffders committed Apr 5, 2018
1 parent c7b8af7 commit 9c87aa3
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/directLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { AjaxResponse, AjaxRequest } from 'rxjs/observable/dom/AjaxObservable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
import { Subscriber } from 'rxjs/Subscriber';
import { Subscription } from 'rxjs/Subscription';
Expand Down Expand Up @@ -102,7 +103,7 @@ export interface OAuth {
contentType: "application/vnd.microsoft.card.oauth",
content: {
text?: string,
name: string,
connectionname: string,
buttons?: CardAction[]
}
}
Expand Down Expand Up @@ -286,12 +287,16 @@ export interface IBotConnection {
activity$: Observable<Activity>,
end(): void,
referenceGrammarId?: string,
postActivity(activity: Activity): Observable<string>
postActivity(activity: Activity): Observable<string>,
getSessionId(): Observable<string>,
onPostActivity: Observable<Activity>
}

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

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

this.onPostActivitySubject = new Subject<Activity>();
this.onPostActivity = this.onPostActivitySubject.asObservable();

if (options.domain)
this.domain = options.domain;
if (options.conversationId) {
Expand Down Expand Up @@ -474,8 +482,35 @@ export class DirectLine implements IBotConnection {
this.tokenRefreshSubscription.unsubscribe();
this.connectionStatus$.next(ConnectionStatus.Ended);
}

getSessionId(): Observable<string> {
// If we're not connected to the bot, get connected
// Will throw an error if we are not connected
konsole.log("getSessionId");
return this.checkConnection(true)
.flatMap(_ =>
Observable.ajax({
method: "GET",
url: `${this.domain}/session/getsessionid`,
withCredentials: true,
timeout,
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${this.token}`
}
})
.map(ajaxResponse => {
konsole.log("getSessionId response: " + ajaxResponse.response.sessionId);
return ajaxResponse.response.sessionId as string;
})
.catch(error => this.catchPostError(error))
)
.catch(error => this.catchExpiredToken(error));
}

postActivity(activity: Activity) {
this.onPostActivitySubject.next(activity);

// Use postMessageWithAttachments for messages with attachments that are local files (e.g. an image to upload)
// Technically we could use it for *all* activities, but postActivity is much lighter weight
// So, since WebChat is partially a reference implementation of Direct Line, we implement both.
Expand Down

0 comments on commit 9c87aa3

Please sign in to comment.