botman/web-widget

Feature request: to add additional parameters to the Chat message

Opened this issue · 3 comments

I am implementing BotMan web-widget plugin for https://www.userfrosting.com/ and building this as a sprinkle (UF's version of the plugin). https://github.com/ssnukala/ufsprinkle-botsevak

I am able to initialize the BotMan web widget and get it to work, but when I send a message the POST is failing because UF checks for CSRF tokens in the post route. I am looking for a way to add additional fields like the CSRF tokens to be added to the POST along with the message and some of the other parameters that are being sent. ssnukala/ufsprinkle-botsevak#1.

I am looking at the following code on BotMan.
https://github.com/botman/web-widget/blob/master/src/chat/botman.ts#L17-L43

    callAPI = (text: string, interactive = false, attachment: IAttachment = null, perMessageCallback: Function, callback: Function) => {
    	let data = new FormData();
    	const postData: { [index: string] : string|Blob } = {
    		driver: 'web',
    		userId: this.userId,
    		message: text,
    		attachment: attachment as Blob,
    		interactive: interactive ? '1' : '0'
    	};

    	Object.keys(postData).forEach(key => data.append(key, postData[key]));

I am thinking 2 potential options
Option 1. along with text add another array called [additionalParams] or something like that to append to the message. additionalParams={}

    callAPI = (text: string, additionalParams={}, interactive = false, attachment: IAttachment = null, perMessageCallback: Function, callback: Function) => {
......
......
    	Object.keys(additionalParams).forEach(key => data.append(key, additionalParams[key]));

Option 2. provide a call back function to append to the POST data before the POST happens beforePostCall: Function

callAPI = (text: string, interactive = false, attachment: IAttachment = null, beforePostCall: Function, perMessageCallback: Function, callback: Function) => {
........
........
    	Object.keys(postData).forEach(key => data.append(key, postData[key]));
			if (beforePostCall) {
					data = beforePostCall(data);
			}

	axios.post(this.chatServer, data).then(response => {.... 

I am thinking Option 2 may be the way to go and will make this very flexible.

This will help dynamically add additional data to the POST message and open this up for additional use cases for a chat based application !

Please let me know if you had a chance to look into this.

I've had the same problem: needed to send extra parameters to server. So I've created a pull request.
Check it out.

I created a new package that solves this and several other issues
https://www.npmjs.com/package/botman-extended-web-widget