This repository was archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 541
Webhook blocks #1621
Merged
Merged
Webhook blocks #1621
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
58731f9
webhook block
64811a8
webhook blocks
8874d38
webhook value allow boolean
497dc9e
resolve changes
10baf8d
nested json payload
a7b5822
eslint
8ab6534
empty payload string
7cdff4f
copywriters
76d87ea
Merge branch 'dev' into webhook-blocks
sam-binary 3cd3afa
modify tooltip
86e52f2
resolve changes
a7c0cb9
radix eslint
dbaaafd
Merge branch 'dev' into webhook-blocks
sam-binary 815aaee
handle empty & add shadow
fd09072
eslint
4d88402
eslint
aaacb6e
eslint
ccab76c
resolve changes
a489b80
Merge branch 'dev' into webhook-blocks
sam-binary 9ae9ced
Merge branch 'dev' into webhook-blocks
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { notify } from '../broadcast'; | ||
import { translate } from '../../../common/i18n'; | ||
|
||
export default Interface => | ||
class extends Interface { | ||
// eslint-disable-next-line class-methods-use-this | ||
sendWebhook(url, payload) { | ||
const onError = () => notify('warn', translate('Unable to send webhook')); | ||
const fetchOption = { | ||
method : 'POST', | ||
mode : 'cors', | ||
headers: { 'Content-Type': 'application/json' }, | ||
}; | ||
|
||
if (payload) { | ||
fetchOption.body = JSON.stringify(payload); | ||
} | ||
|
||
fetch(url, fetchOption) | ||
.then(response => { | ||
if (!response.ok) { | ||
onError(); | ||
} | ||
}) | ||
.catch(onError); | ||
} | ||
|
||
getWebhookInterface() { | ||
return { | ||
sendWebhook: this.sendWebhook, | ||
}; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { translate } from '../../../../../common/i18n'; | ||
|
||
Blockly.Blocks.key_value_pair = { | ||
init() { | ||
this.jsonInit({ | ||
message0: translate('Key: %1 Value: %2'), | ||
args0 : [ | ||
{ | ||
type: 'field_input', | ||
name: 'KEY', | ||
text: 'default', | ||
}, | ||
{ | ||
type: 'input_value', | ||
name: 'VALUE', | ||
}, | ||
], | ||
colour : '#dedede', | ||
output : null, | ||
tooltip: translate('Returns a string representation of a key value pair'), | ||
}); | ||
}, | ||
}; | ||
|
||
Blockly.JavaScript.key_value_pair = block => { | ||
const key = block.getFieldValue('KEY') || ''; | ||
const value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_ATOMIC) || null; | ||
|
||
if (!key) { | ||
return ''; | ||
} | ||
|
||
return [`{"${key}":${value}}`, Blockly.JavaScript.ORDER_ATOMIC]; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import { translate } from '../../../../../common/i18n'; | ||
import { expectValue } from '../shared'; | ||
|
||
Blockly.Blocks.webhook = { | ||
init() { | ||
this.jsonInit({ | ||
message0: translate('Webhook URL: %1'), | ||
args0 : [ | ||
{ | ||
type: 'input_value', | ||
name: 'WEBHOOK_URL', | ||
}, | ||
], | ||
colour : '#dedede', | ||
previousStatement: null, | ||
nextStatement : null, | ||
tooltip : translate('Sends a POST request to a URL'), | ||
}); | ||
|
||
this.itemCount_ = 1; | ||
this.updateShape_(false); | ||
this.setMutator(new Blockly.Mutator(['lists_create_with_item'])); | ||
}, | ||
/** | ||
* Create XML to represent list inputs. | ||
* @return {!Element} XML storage element. | ||
* @this Blockly.Block | ||
*/ | ||
mutationToDom() { | ||
const container = document.createElement('mutation'); | ||
container.setAttribute('items', this.itemCount_); | ||
return container; | ||
}, | ||
/** | ||
* Parse XML to restore the list inputs. | ||
* @param {!Element} xmlElement XML storage element. | ||
* @this Blockly.Block | ||
*/ | ||
domToMutation(xmlElement) { | ||
this.itemCount_ = parseInt(xmlElement.getAttribute('items')); | ||
this.updateShape_(false); | ||
}, | ||
/** | ||
* Populate the mutator's dialog with this block's components. | ||
* @param {!Blockly.Workspace} workspace Mutator's workspace. | ||
* @return {!Blockly.Block} Root block in mutator. | ||
* @this Blockly.Block | ||
*/ | ||
decompose(workspace) { | ||
const containerBlock = workspace.newBlock('lists_create_with_container'); | ||
containerBlock.initSvg(); | ||
|
||
let { connection } = containerBlock.getInput('STACK'); | ||
for (let i = 0; i < this.itemCount_; i++) { | ||
const itemBlock = workspace.newBlock('lists_create_with_item'); | ||
itemBlock.initSvg(); | ||
connection.connect(itemBlock.previousConnection); | ||
connection = itemBlock.nextConnection; | ||
} | ||
return containerBlock; | ||
}, | ||
/** | ||
* Reconfigure this block based on the mutator dialog's components. | ||
* @param {!Blockly.Block} containerBlock Root block in mutator. | ||
* @this Blockly.Block | ||
*/ | ||
compose(containerBlock) { | ||
let itemBlock = containerBlock.getInputTargetBlock('STACK'); | ||
// Count number of inputs. | ||
const connections = []; | ||
while (itemBlock) { | ||
connections.push(itemBlock.valueConnection_); | ||
itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock(); | ||
} | ||
this.itemCount_ = connections.length; | ||
this.updateShape_(true); | ||
}, | ||
/** | ||
* Modify this block to have the correct number of inputs. | ||
* @private | ||
* @this Blockly.Block | ||
*/ | ||
updateShape_(attachInput) { | ||
if (this.itemCount_ && this.getInput('EMPTY')) { | ||
this.removeInput('EMPTY'); | ||
} else if (!this.itemCount_ && !this.getInput('EMPTY')) { | ||
this.appendDummyInput('EMPTY').appendField(translate('Empty payload')); | ||
} | ||
let i; | ||
for (i = 0; i < this.itemCount_; i++) { | ||
if (!this.getInput(`ADD${i}`)) { | ||
const input = this.appendValueInput(`ADD${i}`); | ||
|
||
if (i === 0) { | ||
input.appendField(translate('Payload:')); | ||
} | ||
|
||
if (!attachInput) { | ||
return; | ||
} | ||
const { connection } = input; | ||
const keypair = this.workspace.newBlock('key_value_pair', `keyvalue${i}`); | ||
keypair.initSvg(); | ||
keypair.render(); | ||
keypair.outputConnection.connect(connection); | ||
} | ||
} | ||
// Remove deleted inputs. | ||
while (this.getInput(`ADD${i}`)) { | ||
this.removeInput(`ADD${i}`); | ||
i++; | ||
} | ||
}, | ||
onchange: function onchange(ev) { | ||
if (!this.workspace || this.isInFlyout || this.workspace.isDragging()) { | ||
return; | ||
} | ||
|
||
if (ev.type === Blockly.Events.MOVE) { | ||
for (let i = 0; i < this.itemCount_; i++) { | ||
const currentBlock = this.getInputTargetBlock(`ADD${i}`); | ||
if (currentBlock && currentBlock.type !== 'key_value_pair') { | ||
currentBlock.unplug(true); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
Blockly.JavaScript.webhook = block => { | ||
const url = expectValue(block, 'WEBHOOK_URL'); | ||
|
||
if (!block.itemCount_) { | ||
return `Bot.sendWebhook(${url}, null);\n`; | ||
} | ||
|
||
const keypairs = new Array(block.itemCount_); | ||
for (let i = 0; i < block.itemCount_; i++) { | ||
keypairs[i] = Blockly.JavaScript.valueToCode(block, `ADD${i}`, Blockly.JavaScript.ORDER_ATOMIC) || null; | ||
} | ||
|
||
const params = keypairs | ||
.filter(item => item !== null) | ||
.map(item => { | ||
const regExp = /^{(.*?)}$/; | ||
return item && item.match(regExp)[1]; | ||
}); | ||
|
||
return `Bot.sendWebhook(${url}, {${params}});\n`; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.