├── .env.sample ├── .gitignore ├── docs └── images │ ├── roomId.PNG │ ├── branchSelection.png │ ├── createHerokuApp.png │ ├── herokuConfigVar.PNG │ ├── herokuDeployment.png │ ├── susiChatGitter.PNG │ └── herokuGithubConnect.png ├── kubernetes ├── yamls │ └── generator │ │ ├── 00-namespace.yml │ │ ├── configmap.yml │ │ ├── generator-service.yml │ │ └── generator-deployment.yml ├── travis │ ├── susi-telegrambot-85cf0ec296e9.json.enc │ └── deploy.sh ├── images │ └── generator │ │ ├── setup.sh │ │ └── Dockerfile └── deploy.sh ├── .travis.yml ├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── CONTRIBUTING.md ├── Dockerfile ├── package.json ├── installations └── gce-kubernetes.md ├── README.md ├── index.js └── LICENSE /.env.sample: -------------------------------------------------------------------------------- 1 | ROOM_ID=gitter_room_id 2 | TOKEN=gitter_token -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | config.js 2 | node_modules/ 3 | npm-debug.log 4 | .env 5 | package-lock.json 6 | 7 | -------------------------------------------------------------------------------- /docs/images/roomId.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fossasia/susi_gitterbot/HEAD/docs/images/roomId.PNG -------------------------------------------------------------------------------- /kubernetes/yamls/generator/00-namespace.yml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Namespace 3 | metadata: 4 | name: gitterbot 5 | -------------------------------------------------------------------------------- /docs/images/branchSelection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fossasia/susi_gitterbot/HEAD/docs/images/branchSelection.png -------------------------------------------------------------------------------- /docs/images/createHerokuApp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fossasia/susi_gitterbot/HEAD/docs/images/createHerokuApp.png -------------------------------------------------------------------------------- /docs/images/herokuConfigVar.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fossasia/susi_gitterbot/HEAD/docs/images/herokuConfigVar.PNG -------------------------------------------------------------------------------- /docs/images/herokuDeployment.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fossasia/susi_gitterbot/HEAD/docs/images/herokuDeployment.png -------------------------------------------------------------------------------- /docs/images/susiChatGitter.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fossasia/susi_gitterbot/HEAD/docs/images/susiChatGitter.PNG -------------------------------------------------------------------------------- /docs/images/herokuGithubConnect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fossasia/susi_gitterbot/HEAD/docs/images/herokuGithubConnect.png -------------------------------------------------------------------------------- /kubernetes/yamls/generator/configmap.yml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | metadata: 3 | name: susi-gitterbot 4 | namespace: gitterbot 5 | kind: ConfigMap 6 | -------------------------------------------------------------------------------- /kubernetes/travis/susi-telegrambot-85cf0ec296e9.json.enc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fossasia/susi_gitterbot/HEAD/kubernetes/travis/susi-telegrambot-85cf0ec296e9.json.enc -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 6.10.2 4 | sudo: required 5 | services: 6 | - docker 7 | env: 8 | - PATH=$PATH:${HOME}/google-cloud-sdk/bin CLOUDSDK_CORE_DISABLE_PROMPTS=1 9 | after_success: 10 | - bash kubernetes/travis/deploy.sh -------------------------------------------------------------------------------- /kubernetes/images/generator/setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | git clone ${REPOSITORY} susi_gitterbot 3 | cd susi_gitterbot 4 | git checkout ${BRANCH} 5 | 6 | if [ -v COMMIT_HASH ]; then 7 | git reset --hard ${COMMIT_HASH} 8 | fi 9 | 10 | rm -rf .git 11 | npm install --no-shrinkwrap 12 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | **I'm submitting a ...** 3 | - [ ] bug report 4 | - [ ] feature request 5 | 6 | **Current behavior:** 7 | 8 | 9 | **Expected behavior:** 10 | 11 | 12 | **Steps to reproduce:** 13 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:boron 2 | 3 | # Create app directory 4 | RUN mkdir -p /usr/src/app 5 | WORKDIR /usr/src/app 6 | 7 | # Install app dependencies 8 | COPY package.json /usr/src/app 9 | RUN npm install 10 | 11 | # Bundle app source 12 | COPY . /usr/src/app 13 | 14 | EXPOSE 8080 15 | CMD [ "npm", "start" ] -------------------------------------------------------------------------------- /kubernetes/yamls/generator/generator-service.yml: -------------------------------------------------------------------------------- 1 | kind: Service 2 | apiVersion: v1 3 | metadata: 4 | name: susi-gitterbot 5 | namespace: gitterbot 6 | spec: 7 | ports: 8 | - port: 8080 9 | protocol: TCP 10 | targetPort: 8080 11 | selector: 12 | app: susi-gitterbot 13 | type: LoadBalancer -------------------------------------------------------------------------------- /kubernetes/deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export DIR=${BASH_SOURCE%/*} 3 | 4 | if [ "$1" = "delete" ]; then 5 | echo "Clearing the cluster." 6 | kubectl delete -f ${DIR}/yamls/generator 7 | echo "Done. The project was removed from the cluster." 8 | elif [ "$1" = "create" ]; then 9 | echo "Deploying the project to kubernetes cluster" 10 | kubectl create -f ${DIR}/yamls/generator 11 | echo "Done. The project was deployed to kubernetes. :)" 12 | fi 13 | -------------------------------------------------------------------------------- /kubernetes/images/generator/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:boron 2 | 3 | ARG COMMIT_HASH 4 | ARG BRANCH 5 | ARG REPOSITORY 6 | 7 | ENV COMMIT_HASH ${COMMIT_HASH:-null} 8 | ENV BRANCH ${BRANCH:-master} 9 | ENV REPOSITORY ${REPOSITORY:-https://github.com/fossasia/susi_gitterbot.git} 10 | ENV INSTALL_PATH /gitter 11 | 12 | RUN mkdir -p $INSTALL_PATH 13 | 14 | WORKDIR $INSTALL_PATH 15 | 16 | COPY . . 17 | 18 | RUN bash setup.sh 19 | 20 | WORKDIR $INSTALL_PATH/susi_gitterbot 21 | 22 | CMD [ "npm", "start" ] 23 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | Fixes # 3 | 4 | #### Checklist 5 | 6 | - [ ] I have read the [Contribution & Best practices Guide](https://blog.fossasia.org/open-source-developer-guide-and-best-practices-at-fossasia) and my PR follows them. 7 | - [ ] My branch is up-to-date with the Upstream `master` branch. 8 | - [ ] I have added necessary documentation (if appropriate) 9 | 10 | #### Changes proposed in this pull request: 11 | 12 | - 13 | - 14 | -------------------------------------------------------------------------------- /kubernetes/yamls/generator/generator-deployment.yml: -------------------------------------------------------------------------------- 1 | kind: Deployment 2 | apiVersion: apps/v1beta1 3 | metadata: 4 | name: susi-gitterbot 5 | namespace: gitterbot 6 | spec: 7 | replicas: 1 8 | template: 9 | metadata: 10 | labels: 11 | app: susi-gitterbot 12 | spec: 13 | containers: 14 | - name: susi-gitterbot 15 | image: fossasia/susi_gitterbot:latest-master 16 | ports: 17 | - containerPort: 8080 18 | protocol: TCP 19 | envFrom: 20 | - configMapRef: 21 | name: susi-gitterbot 22 | restartPolicy: Always 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "susi_gitterbot", 3 | "version": "1.0.0", 4 | "description": "Integration of Susi AI to Gitter messenger", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/fossasia/susi_gitterbot.git" 12 | }, 13 | "dependencies": { 14 | "dotenv": "^5.0.1", 15 | "http": "0.0.0", 16 | "https": "^1.0.0", 17 | "request": "^2.81.0" 18 | }, 19 | "author": "", 20 | "license": "ISC", 21 | "bugs": { 22 | "url": "https://github.com/fossasia/susi_gitterbot/issues" 23 | }, 24 | "homepage": "https://github.com/fossasia/susi_gitterbot#readme" 25 | } 26 | -------------------------------------------------------------------------------- /kubernetes/travis/deploy.sh: -------------------------------------------------------------------------------- 1 | export DEPLOY_BRANCH=${DEPLOY_BRANCH:-master} 2 | 3 | export REPOSITORY="https://github.com/${TRAVIS_REPO_SLUG}.git" 4 | 5 | if [ "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_REPO_SLUG" != "fossasia/susi_gitterbot" -o "$TRAVIS_BRANCH" != "$DEPLOY_BRANCH" ]; then 6 | echo "Skip production deployment for a very good reason." 7 | exit 0 8 | fi 9 | 10 | echo ">>> Removing gcoud files" 11 | sudo rm -f /usr/bin/git-credential-gcloud.sh 12 | sudo rm -f /usr/bin/bq 13 | sudo rm -f /usr/bin/gsutil 14 | sudo rm -f /usr/bin/gcloud 15 | rm -rf node_modules 16 | 17 | echo ">>> Installing new files" 18 | curl https://sdk.cloud.google.com | bash; 19 | source ~/.bashrc 20 | gcloud components install kubectl 21 | 22 | echo ">>> Decrypting credentials and authenticating gcloud account" 23 | gcloud config set compute/zone us-central1-b 24 | openssl aes-256-cbc -K $encrypted_4ece7e5e9ee0_key -iv $encrypted_4ece7e5e9ee0_iv -in ./kubernetes/travis/susi-telegrambot-85cf0ec296e9.json.enc -out susi-telegrambot-85cf0ec296e9.json -d 25 | mkdir -p lib 26 | gcloud auth activate-service-account --key-file susi-telegrambot-85cf0ec296e9.json 27 | export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/susi-telegrambot-85cf0ec296e9.json 28 | gcloud config set project susi-telegrambot 29 | gcloud container clusters get-credentials bots 30 | echo ">>> Building Docker image" 31 | cd kubernetes/images/generator 32 | docker build --build-arg COMMIT_HASH=$TRAVIS_COMMIT --build-arg BRANCH=$DEPLOY_BRANCH --build-arg REPOSITORY=$REPOSITORY --no-cache -t fossasia/susi_gitterbot:$TRAVIS_COMMIT . 33 | docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" 34 | docker tag fossasia/susi_gitterbot:$TRAVIS_COMMIT fossasia/susi_gitterbot:latest-$DEPLOY_BRANCH 35 | echo ">>> Pushing docker image" 36 | docker push fossasia/susi_gitterbot 37 | echo ">>> Updating deployment" 38 | kubectl set image deployment/susi-gitterbot --namespace=gitterbot susi-gitterbot=fossasia/susi_gitterbot:$TRAVIS_COMMIT 39 | rm -rf $GOOGLE_APPLICATION_CREDENTIALS -------------------------------------------------------------------------------- /installations/gce-kubernetes.md: -------------------------------------------------------------------------------- 1 | ## Setup: 2 | 3 | - If you don't already have a Google Account (Gmail or Google Apps), you must [create one](https://accounts.google.com/SignUp). Then, sign-in to Google Cloud Platform console ( [cloud.google.com](http://console.cloud.google.com/)) and create a new project. 4 | 5 | - Subscribe [free tier](https://cloud.google.com/free/) for 12 months.Next, [enable billing](https://console.cloud.google.com/billing) in the Cloud Console in order to use Google Cloud resources and [enable the Container Engine API](https://console.cloud.google.com/project/_/kubernetes/list). 6 | 7 | - Install [Docker](https://docs.docker.com/engine/installation/), and [Google Cloud SDK](https://cloud.google.com/sdk/). 8 | - Finally, after Google Cloud SDK installs, run the following command to install kubectl: 9 | gcloud components install kubectl 10 | 11 | - Choose a [Google Cloud Project zone](https://cloud.google.com/compute/docs/regions-zones/regions-zones) to run your service. We will be using us-central1. This is configured on the command line via: 12 | 13 | gcloud config set compute/zone us-central1 14 | 15 | 16 | ## Deployment: 17 | 18 | - First create a cluster 19 | 20 | gcloud container clusters create clusterName 21 | 22 | - In gcloud shell run the following command to deploy application using given configurations. 23 | 24 | bash ./kubernetes/deploy.sh create 25 | 26 | - This will create the deployment according to yaml files define. 27 | 28 | - The Kubernetes master creates the load balancer and related Compute Engine forwarding rules, target pools, and firewall rules to make the service fully accessible from outside of Google Cloud Platform. 29 | 30 | - Wait for a few minutes for all the containers to be created. 31 | 32 | ## Tracking: 33 | 34 | - To track the progress using the Web GUI run 35 | 36 | kubectl proxy 37 | 38 | - After that goto [http://localhost:8001/ui](http://localhost:8001/ui) 39 | 40 | ## Cleanup: 41 | 42 | - If you want to delete the deployment from the cluster enter this command: 43 | 44 | bash ./kubernetes/deploy.sh delete 45 | 46 | 47 | - To delete cluster enter: 48 | 49 | gcloud container clusters delete clusterName 50 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributions Best Practices 2 | 3 | **Commits** 4 | * Write clear meaningful git commit messages (Do read http://chris.beams.io/posts/git-commit/) 5 | * Make sure your PR's description contains GitHub's special keyword references that automatically close the related issue when the PR is merged. (More info at https://github.com/blog/1506-closing-issues-via-pull-requests ) 6 | * When you make very very minor changes to a PR of yours (like for example fixing a failing travis build or some small style corrections or minor changes requested by reviewers) make sure you squash your commits afterwards so that you don't have an absurd number of commits for a very small fix. (Learn how to squash at https://davidwalsh.name/squash-commits-git ) 7 | * When you're submitting a PR for a UI-related issue, it would be really awesome if you add a screenshot of your change or a link to a deployment where it can be tested out along with your PR. It makes it very easy for the reviewers and you'll also get reviews quicker. 8 | 9 | **Code Styleguide** 10 | * Do follow the .editorconfig file regarding maintaining of code style (It's mandatory). 11 | * For more information regarding .editorconfig file, see [editorconfig](http://editorconfig.org/#download) 12 | 13 | **Feature Requests and Bug Reports** 14 | * When you file a feature request or when you are submitting a bug report to the [issue tracker](https://github.com/fossasia/susi_gitterbot/issues), make sure you add steps to reproduce it. Especially if that bug is some weird/rare one. 15 | 16 | **Join the development** 17 | * Before you join development, please set up the project on your local machine, run it and go through the application completely. Press on any button you can find and see where it leads to. Explore. (Don't worry ... Nothing will happen to the app or to you due to the exploring :wink: Only thing that will happen is, you'll be more familiar with what is where and might even get some cool ideas on how to improve various aspects of the app.) 18 | * If you would like to work on an issue, drop in a comment at the issue. If it is already assigned to someone, but there is no sign of any work being done, please free to drop in a comment so that the issue can be assigned to you if the previous assignee has dropped it entirely. 19 | 20 | Do read the [Open Source Developer Guide and Best Practices at FOSSASIA](https://blog.fossasia.org/open-source-developer-guide-and-best-practices-at-fossasia). 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started : Gitter Susi AI Bot Installation 2 | 3 | Create a github or twitter account. Now you need to sign in to Gitter with a twitter or github account from [here](https://gitter.im). 4 | 5 | We will be using the streaming API of Gitter to deploy SUSI chat bot to Gitter. You can read the [documentation](https://developer.gitter.im/docs/streaming-api) and get familiar with the API. 6 | 7 | [![Deploy to Docker Cloud](https://files.cloud.docker.com/images/deploy-to-dockercloud.svg)](https://cloud.docker.com/stack/deploy/?repo=https://github.com/fossasia/susi_gitterbot) 8 | 9 | ## Prerequisites 10 | To create your account on -: 11 | 1. Github 12 | 2. Gitter 13 | 3. Heroku 14 | 15 | ## SUSI.AI Gitter messenger bot 16 | 17 | Feel free to play around with the already built [SUSI.AI bot](https://gitter.im/susiaitest/Lobby) on Gitter. This room will reply to the queries when mentioned with "@susiai". So we can try asking SUSI.AI our queries, for example - @susiai BTC to INR [here](https://gitter.im/susiaitest/Lobby). 18 | 19 | ## Setup your own Messenger Bot 20 | 21 | 1. Fork this repository. 22 | 23 | 2. Create your community by visiting this [page](https://gitter.im/home/explore#createcommunity). After writing your community name press next, invite the people you want to be in this room and press next. You will be redirected to your communities lobby. This lobby is the chat room to which we will deploy our SUSI AI. 24 | 25 | 3. Now visit the gitter developer [page](https://developer.gitter.im/docs/welcome), press sign in on the top right. You will be redirected to your apps page. Copy the personal access token written there. 26 | 27 | 4. On a new tab, in your browser visit https://api.gitter.im/v1/rooms?access_token=YOUR_ACCESS_TOKEN, with YOUR_ACCESS_TOKEN replaced by the token we just copied. 28 | A JSON object will be shown on our browser screen. You will see the value of 'name' key as YOUR_COMMUNITY_NAME/Lobby. Copy the id of this chat room, as we will need it later. You can refer to the image below, you will have your chat room id in the area coloured black. 29 | 30 | alt text 31 | 32 | 5. Create a new heroku app [here](https://dashboard.heroku.com/new?org=personal-apps). 33 | 34 | This app will accept the requests from Gitter and Susi api. 35 | 36 | alt text 37 | 38 | 6. Connect the heroku app to the forked repository. 39 | 40 | Connect the app to Github 41 | 42 | alt text 43 | 44 | 7. Set the config variables for this heroku app in the setting tab of your account. Set ROOM_ID to the id of the chat room and TOKEN to the personal access token. 45 | 46 | alt text 47 | 48 | 8. Deploy on development branch. If you intend to contribute, it is recommended to Enable Automatic Deploys. 49 | 50 | Branch Deployment. 51 | 52 | alt text 53 | 54 | Successful Deployment. 55 | 56 | alt text 57 | 58 | 9. Go to the your Gitter room created and enjoy chatting with Susi. 59 | 60 | alt text 61 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config() 2 | var https = require('https'); 3 | var request = require('request'); 4 | var http = require("http"); 5 | var roomId = process.env.ROOM_ID || config.ROOM_ID; 6 | var token = process.env.TOKEN || config.TOKEN; 7 | var emptyMessage = " \n"; 8 | var susiUsername=""; 9 | 10 | // To bind a port on heroku 11 | https.createServer(function (request, response) { 12 | console.log("listening on port "+(process.env.PORT || 8080)); 13 | }).listen(process.env.PORT || 8080); 14 | // ping heroku every 10 minutes to prevent it from sleeping 15 | setInterval(function() { 16 | http.get(process.env.HEROKU_URL); 17 | }, 600000); // every 10 minutes 18 | 19 | const timezoneOffset = (new Date()).getTimezoneOffset(); 20 | const defaultAnswer = { 21 | data: [{ 22 | "0": "", 23 | timezoneOffset, 24 | language: "en" 25 | }], 26 | metadata: { 27 | count: 1 28 | }, 29 | actions: [{ 30 | type: "answer", 31 | expression: "Hmm... I\'m not sure if i understand you correctly." 32 | }], 33 | skills: ["/en_0090_fail.json"], 34 | persona: {} 35 | }; 36 | // Setting the options variable to use it in the https request block 37 | var options = { 38 | hostname: 'stream.gitter.im', 39 | port: 443, 40 | path: '/v1/rooms/' + roomId + '/chatMessages', 41 | method: 'GET', 42 | headers: {'Authorization': 'Bearer ' + token} 43 | }; 44 | //make api call to get user name 45 | var gitterOptionsUsername = { 46 | method: 'GET', 47 | url: "https://api.gitter.im/v1/user", 48 | headers: 49 | { 50 | 'authorization': 'Bearer '+ token , 51 | 'content-type': 'application/json', 52 | 'accept': 'application/json' 53 | }, 54 | json: true 55 | }; 56 | // making the request to Gitter API 57 | request(gitterOptionsUsername, function (error, response, body) { 58 | if(error) 59 | throw new Error(error); 60 | susiUsername=body[0].username; 61 | }); 62 | function sendAnswer(ans){ 63 | // To set options to send a message i.e. the reply by Susi AI to client's message, to Gitter 64 | var gitterOptions = { 65 | method: 'POST', 66 | url: 'https://api.gitter.im/v1/rooms/'+roomId+'/chatMessages', 67 | headers: 68 | { 69 | 'authorization': 'Bearer '+ token , 70 | 'content-type': 'application/json', 71 | 'accept': 'application/json' 72 | }, 73 | body: 74 | { 75 | text: ans 76 | }, 77 | json: true 78 | }; 79 | 80 | // making the request to Gitter API 81 | request(gitterOptions, function (error, response, body) { 82 | if(error) 83 | throw new Error(error); 84 | }); 85 | } 86 | function getSusiAnswer(clientMsg){ 87 | var ans = ''; 88 | // To set options for a request to Susi with the client message as a query 89 | var susiOptions = { 90 | method: 'GET', 91 | url: 'http://api.susi.ai/susi/chat.json', 92 | qs: { q: clientMsg } 93 | }; 94 | 95 | // A request to the Susi AI API 96 | request(susiOptions, function (error1, response1, body1) { 97 | if (error1) 98 | throw new Error(error1); 99 | 100 | var data = JSON.parse(body1); 101 | //handle default case of no answer 102 | if(!data.answers.length || data.answers.length===0){ 103 | data.answers.push(defaultAnswer); 104 | } 105 | var answerActions = data.answers[0].actions; 106 | answerActions.forEach(function(action) { 107 | var type=action.type; 108 | // fetching the answer from Susi's response 109 | if(type === "rss"){ 110 | ans += "I found this on the web-:\n\n"; 111 | var maxCount=5; 112 | let rssData = JSON.parse(body1).answers[0].data; 113 | let columns = type[1]; 114 | let key = Object.keys(columns); 115 | ans = ""; 116 | rssData.forEach(function(row,index) { 117 | if(row