Skip to content

Instantly share code, notes, and snippets.

View jcabot's full-sized avatar

Jordi Cabot jcabot

View GitHub Profile
@jcabot
jcabot / toxic_comments_wp.php
Last active May 30, 2019 08:52
Tensorflow.js toxicity model used to analyse WordPress comments
public function comment_toxicity()
{
$options = get_option( 'settingsToxic' );
$threshold = $options['threshold']/100;
if(is_single() && comments_open() && isset($options['toxicdetection']) ) {
?>
<script>
window.onload=function() {
var commentForm = document.getElementById('commentform');
commentForm.addEventListener('submit', function(event){
@jcabot
jcabot / Gist-GithubStargazer.execution
Last active October 24, 2019 16:05
GitHub stargazer
use provider GithubPlatform.GithubWebhookEventProvider
on event Star_Created do
action SlackPlatform.PostMessage(message : ":tada: New star on <" + context(star).get("repository->html_url") + " | " + context(star).get("repository->name") + "> by <" + context(star).get("sender->html_url") + " | " + context(star).get("sender->login") + "> :confetti_ball:\nCurrent stargazers count: " + context(star).get("repository->stargazers_count") + ":clap::champagne:", channel : config(slack.channel))
on event Star_Deleted do
action SlackPlatform.PostMessage(message : ":sob: <" + context(star).get("sender->html_url") + " | " + context(star).get("sender->login") + "> unstarred <" + context(star).get("repository->html_url") + " | " + context(star).get("repository->name") + "> :face_with_head_bandage: let's forget about it and build awesome features! :kissing_heart:\nCurrent stargazer count: " + context(star).get("repository->stargazers_count") + ":stars:", channel : config(slack.channel))
@jcabot
jcabot / random.execution
Created November 27, 2019 05:42
Introducing randomness in the chatbot reply
on intent Welcome do
switch(value : Math.random) {
case value <= 0.25 : ChatPlatform.Reply("Hi")
case value <= 0.5 && value > 0.25 : ChatPlatform.Reply("Hello")
case value <= 0.75 && value > 0.5 : ChatPlatform.Reply("Hi, nice to meet you!")
case value <= 1 && value > 0.75 : ChatPlatform.Reply("Greetings")
}
@jcabot
jcabot / livechattransfer.execution
Created December 18, 2019 15:00
Excerpt showing how to transfer the control of the communication from a chatbot to a human
on intent Default_Fallback_Intent from ReactPlatform do
val reactChannel = context.get("chat").get("channel") as String
var reactSlackMap = session.get("react-slack-map") as com.google.common.collect.BiMap<String, String>
if(reactSlackMap === null) {
reactSlackMap = com.google.common.collect.HashBiMap.<String, String>create
session.put("react-slack-map", reactSlackMap)
}
val storedSlackTs = reactSlackMap.get(reactChannel)
@jcabot
jcabot / livechattobottransfer.execution
Created December 18, 2019 15:11
live chat to chatbot transfer communication
on intent Default_Fallback_Intent from SlackPlatform do
val reactSlackMap = session.get("react-slack-map") as com.google.common.collect.BiMap<String, String>
if(reactSlackMap !== null) {
val slackTs = context.get("slack").get("threadTs") as String
val reactChannel = reactSlackMap.inverse.get(slackTs)
if(reactChannel === null) {
if(slackTs.empty) {
/*
* No threadTs means we are in the top-level conversation
*/
@jcabot
jcabot / XatkitSentimentAnalysis
Last active March 22, 2020 18:49
Xatkit integration with Stanford NLP
public RecognizedIntent process(RecognizedIntent recognizedIntent, XatkitSession session) {
session.getRuntimeContexts().setContextValue(NLP_CONTEXT_KEY, 1, SENTIMENT_PARAMETER_KEY,
DEFAULT_SENTIMENT_VALUE);
Annotation annotation = getAnnotation(recognizedIntent.getMatchedInput(), session);
List<CoreMap> sentenceAnnotations = annotation.get(CoreAnnotations.SentencesAnnotation.class);
String sentimentValue =
sentenceAnnotations.get(sentenceAnnotations.size() - 1).get(SentimentCoreAnnotations.SentimentClass.class);
session.getRuntimeContexts().setContextValue(NLP_CONTEXT_KEY, 1, SENTIMENT_PARAMETER_KEY, sentimentValue);
return recognizedIntent;
@jcabot
jcabot / XatkitStanfordNLPWrapper
Created March 22, 2020 19:06
Text annotation wrapper for Stanford NLP in Xatkit
protected final Annotation getAnnotation(String input, XatkitSession session) {
String nlpInput = (String) session.get(NLP_INPUT_SESSION_KEY);
Annotation annotation = (Annotation) session.get(NLP_ANNOTATION_SESSION_KEY);
if (isNull(annotation) || isNull(nlpInput) || !nlpInput.equals(input)) {
Log.debug("There is no annotation for \"{0}\" in the session, computing the annotation with {1}", input,
StanfordNLPService.class.getSimpleName());
annotation = StanfordNLPService.getInstance().annotate(input);
session.store(NLP_INPUT_SESSION_KEY, input);
session.store(NLP_ANNOTATION_SESSION_KEY, annotation);
} else {
@jcabot
jcabot / wp_nav_tab_registering.php
Created April 16, 2020 10:41
Registering plugin settings
register_setting(
'settingsXatkitPlugin_group',
'settingsXatkitPlugin');
register_setting(
'settingsXatkitDisplay_group',
'settingsXatkitDisplay');
@jcabot
jcabot / wp_nav_tab_sections_fields
Created April 16, 2020 10:46
Defining the sections and settings fields and linking them to the tag where they will be displayed
//Defining the fields in each group
add_settings_section(
'configuration',
'Let Xatkit and WordPress be friends',
false, 'xatkit-configuration'
);
add_settings_field(
'enableXatkit',
'Start communicating with Xatkit',
array($this,'render_enableXatkit_field'),
@jcabot
jcabot / wp_nav_tab_rendering_field
Created April 16, 2020 10:55
Callback function to render a settings field
public function render_widgetTitle_field() {
// Retrieve the full set of options in the group and access the individual option from the array
$options = get_option( 'settingsXatkitDisplay' );
$value = isset( $options['widgetTitle'] ) ? $options['widgetTitle'] : 'Chat with us';
echo '<input type="text" name="settingsXatkitDisplay[widgetTitle]" size="50" value="' . esc_attr( $value ).'" />';
}