Skip to content

Instantly share code, notes, and snippets.

@ronoaldo
Last active November 1, 2021 13:50
Show Gist options
  • Save ronoaldo/4a2b642823d19cb327bdbc20acf50b45 to your computer and use it in GitHub Desktop.
Save ronoaldo/4a2b642823d19cb327bdbc20acf50b45 to your computer and use it in GitHub Desktop.
Sample Dialogflow CX client library in Bash
#!/bin/bash
# Copyright 2021 Ronoaldo JLP <ronoaldo@gmail.com>
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# Use Default Application Credentials - Required!
export GOOGLE_APPLICATION_CREDENTIALS=./credentials.json
if ! [ -f $GOOGLE_APPLICATION_CREDENTIALS ] ; then
echo "Missing $GOOGLE_APPLICATION_CREDENTIALS"
exit 1
fi
PROJECT=${PROJECT:-$(gcloud config get-value project 2>/dev/null)}
ENDPOINT="https://dialogflow.googleapis.com/v3/"
token() {
gcloud auth application-default print-access-token
}
get() {
curl -s -L -X GET -H "Authorization: Bearer $(token)" "${ENDPOINT}${1}"
}
post() {
curl -L -X POST -H "Authorization: Bearer $(token)" -H "Content-Type: application/json" -d @"$2" "${ENDPOINT}${1}"
}
patch() {
curl -L -X PATCH -H "Authorization: Bearer $(token)" -H "Content-Type: application/json" -d @"$2" "${ENDPOINT}${1}"
}
save_entities() {
while read entity ; do
name="$(jq -r '.name' <<<"$entity")"; name="${name##*/}"
display_name="$(jq -r '.displayName' <<<"$entity")"
mkdir -p $1
jq '.' > $1/$name <<<"$entity"
done
}
usage() {
cat <<EOF
Usage:
dialogflow.sh agents export
Export all agents for the current project ($PROJECT) in the agents/ folder
dialogflow.sh intents export AGENT_NAME
Export all intents for the provided agent into agents/AGENT_NAME.intents/
dialogflow.sh intents import INTENT_FILE
Import the provided intent using the intent.name attribute from the file itself.
EOF
}
case $1 in
agents)
case $2 in
export)
get projects/$PROJECT/locations/global/agents | jq -c '.agents[]' | save_entities "agents"
;;
*) usage ;;
esac ;;
intents)
case $2 in
export)
agent="${3##*/}"
if [ x"$agent" == x"" ] ; then
echo "Please provide the agent UUID to export intents"
exit 1
fi
get projects/$PROJECT/locations/global/agents/${agent}/intents | jq -c '.intents[]' | save_entities "agents/${agent}.intents/"
;;
import)
shift ; shift
while [ $# -gt 0 ] ; do
patch "$(jq -r '.name' <"$1")" "$1"
shift
done
;;
*) usage ;;
esac ;;
*) usage ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment