postActivity()

in src/directLine.ts [756:787]


    postActivity(activity: Activity) {
        // If user id is set, check if it match activity.from.id and always override it in activity
        if (this.userIdOnStartConversation && activity.from && activity.from.id !== this.userIdOnStartConversation) {
            console.warn('DirectLineJS: Activity.from.id does not match with user id, ignoring activity.from.id');
            activity.from.id = this.userIdOnStartConversation;
        }
        // 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.
        if (activity.type === "message" && activity.attachments && activity.attachments.length > 0)
            return this.postMessageWithAttachments(activity);

        // If we're not connected to the bot, get connected
        // Will throw an error if we are not connected
        konsole.log("postActivity", activity);
        return this.checkConnection(true)
        .flatMap(_ =>
            this.services.ajax({
                method: "POST",
                url: `${this.domain}/conversations/${this.conversationId}/activities`,
                body: activity,
                timeout: this.timeout,
                headers: {
                    "Content-Type": "application/json",
                    ...this.commonHeaders()
                },
            })
            .map(ajaxResponse => ajaxResponse.response.id as string)
            .catch(error => this.catchPostError(error))
        )
        .catch(error => this.catchExpiredToken(error));
    }