postActivity()

in src/directLineStreaming.ts [188:215]


  postActivity(activity: Activity) {
    if (activity.type === "message" && activity.attachments && activity.attachments.length > 0) {
      return this.postMessageWithAttachments(activity);
    }

    const resp$ = Observable.create(async subscriber => {
      const request = BFSE.StreamingRequest.create('POST', '/v3/directline/conversations/' + this.conversationId + '/activities');
      request.setBody(JSON.stringify(activity));
      const resp = await this.streamConnection.send(request);

      try {
        if (resp.statusCode !== 200) throw new Error("PostActivity returned " + resp.statusCode);
        const numberOfStreams = resp.streams.length;
        if (numberOfStreams !== 1) throw new Error("Expected one stream but got " + numberOfStreams)
        const idString = await resp.streams[0].readAsString();
        const {Id : id} = JSON.parse(idString);
        return subscriber.next(id);
      } catch(e) {
          // If there is a network issue then its handled by
          // the disconnectionHandler. Everything else can
          // be retried
          console.warn(e);
          this.streamConnection.disconnect();
          return subscriber.error(e);
      }
    });
    return resp$;
  }