{"id":13513767,"url":"https://github.com/omatt/messenger-chatbot","last_synced_at":"2025-03-31T02:32:55.935Z","repository":{"id":41721252,"uuid":"101009294","full_name":"omatt/messenger-chatbot","owner":"omatt","description":" Facebook Messenger chat bot using Heroku + Firebase","archived":true,"fork":false,"pushed_at":"2022-12-07T15:37:33.000Z","size":305,"stargazers_count":11,"open_issues_count":3,"forks_count":9,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-08-02T05:09:05.391Z","etag":null,"topics":["chatbot","firebase","javascript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/omatt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-22T01:53:40.000Z","updated_at":"2023-12-25T06:32:07.000Z","dependencies_parsed_at":"2023-01-24T18:04:25.242Z","dependency_job_id":null,"html_url":"https://github.com/omatt/messenger-chatbot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omatt%2Fmessenger-chatbot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omatt%2Fmessenger-chatbot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omatt%2Fmessenger-chatbot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omatt%2Fmessenger-chatbot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/omatt","download_url":"https://codeload.github.com/omatt/messenger-chatbot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222615148,"owners_count":17012022,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatbot","firebase","javascript"],"created_at":"2024-08-01T05:00:37.410Z","updated_at":"2024-11-01T17:30:41.110Z","avatar_url":"https://github.com/omatt.png","language":"JavaScript","funding_links":[],"categories":["BOT"],"sub_categories":[],"readme":"\n---\n\n**NOTE:**\n\nThis sample project uses Heroku's free-tier to host the chat bot. [Heroku's pricing change](https://blog.heroku.com/next-chapter) will be removing their free-tier and there are no plans to move this sample chat bot project to a paid-tier. While this project can still be used, the project will **no longer be actively maintained**.\n\n---\n\n# messenger-chatbot\nA simple Facebook Messenger chat bot using Heroku + Firebase\n\n![](https://github.com/omatt/messenger-chatbot/blob/master/res/heroku_firebase.png \"Heroku + Firebase\")\n\nQ: Why not just use one or the other?\n\nA: Hosting Messenger chatbot using Firebase Functions would require external invocations - which is currently only available on [Paid Plan](https://firebase.google.com/pricing/). See Functions \u003e Outbound Networking. Also, I want to demonstrate Firebase Realtime Database using javascript.\n\nTry [Magic X Ball](https://m.me/magicxball) Messenger Bot now.\n\n![](https://github.com/omatt/messenger-chatbot/blob/master/res/sample_chat.PNG \"is the weather good today?\")\n\n## Setup\n1. Install [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli). Create an account on [Heroku](https://www.heroku.com/) if you don't have one.\n2. Install [nodejs](https://nodejs.org/en/download/). Open your terminal and make sure that you have the updated version. If you're having issues with your npm version, you may use nvm and follow this guide [here](https://github.com/creationix/nvm)\n\n    ```\n    sudo npm install -g npm \n    npm -v       // Check npm version\n    ```\n3. Create a folder for your project and create a initialize a new Node project.\n\n    ```\n    npm init\n    ```\n4. Install additional Node dependencies. Express is for the server, request is for sending out messages, and body-parser is to process messages.\n\n    ```\n    npm install express request body-parser --save\n    ```\n5. Follow steps on [Facebook docs](https://developers.facebook.com/docs/messenger-platform/guides/quick-start) for the setup of your Messenger chatbot and getting the required API Keys. \n\n6. You may use the index.js on this project as a base and fill the required API Keys for your app to work.\n\n    ``` javascript\n    const token = \"FB_PAGE_ACCESS_TOKEN\"\n    ```\n    Get the Page Access Token by following the Step 3 of the [docs](https://developers.facebook.com/docs/messenger-platform/guides/quick-start).\n\n7. This app mimics a Magic 8 Ball. The bot will only accept answers when called as 'magic ball' or 'magicball'. An answer will be detected if the message from the user has a '?' at the end of the sentence.\n\n    ``` javascript\n    var engaged = false;  // set as false by default\n    if (messageText) {\n      // If we receive a text message, check to see if it matches a keyword\n      // and send back an answer. Otherwise, ask the question again.\n      if (messageText.toLowerCase().replace(/\\s+/g, '')includes(\"magicball\")){\n        engaged = true; // Start accepting questions\n        sendTextMessage(senderID, \"Yes?\");\n      } else if (engaged \u0026\u0026 messageText.toLowerCase().includes(\"?\")) {\n        engaged = false;  // Once question is answered - set to false again\n        sendAnswer(senderID);\n      } else if (engaged){\n        sendTextMessage(senderID, \"Could you repeat your question again?\");\n      }\n    } else if (messageAttachments) {\n      sendTextMessage(senderID, \"Message with attachment received\");\n    }\n    ```\n\n8. For Firebase, currently, we'll only be using Realtime Database to fetch data. Create an account on [Firebase](https://firebase.google.com/) if you don't have one.\n\n9. For Realtime Datbase, you may follow the quickstart guide [here](https://firebase.google.com/docs/database/web/start). You can find your Realtime Database URL in the Database tab in the [Firebase console](https://console.firebase.google.com). It will be in the form of https://\u003cdatabaseName\u003e.firebaseio.com.\n\n    ``` javascript\n    /** Firebase **/\n    var firebase = require(\"firebase\");\n    // Set the configuration for your app\n    var firebaseConfig = {\n      apiKey: \"FIREBASE_API_KEY\",  // Firebase Console \u003e Project \u003e Settings \u003e Web API Key\n      authDomain: \"\u003cAPP_CODE\u003e.firebaseapp.com\",\n      databaseURL: \"https://\u003cAPP_CODE\u003e.firebaseio.com\",\t// This chatbot only utilizes Firebase RTDB\n      storageBucket: \"\u003cAPP_CODE\u003e.appspot.com\"\n    };\n    firebase.initializeApp(firebaseConfig);\n    // Get a reference to the database service\n    var database = firebase.database();\n    ```\n    \n10. Reading from Firebase Realtime Database. You may read the docs [here](https://firebase.google.com/docs/database/web/read-and-write#read_data_once).\n\n    ``` javascript\n    function sendAnswer(senderID){\n      let listAnswers = [];\n      database.ref('xball/answers').once('value').then(function(snapshot) {\n        snapshot.forEach(function(childSnapshot) {\n          var answer = {};\t// initialize new object\n          // key\n          answer['answerType'] = childSnapshot.key;\n          // childData\n          answer['answerText'] = childSnapshot.val();\n          listAnswers.push(answer);\n          console.log(\"sendAnswer \" + answer.answerType + \" \" + answer.answerText);\n        });\n        let min = Math.ceil(0), max = Math.floor(Object.keys(listAnswers).length);\n        // Generate random num\n        let randNum = Math.floor(Math.random() * (max - min + 1)) + min;\n        console.log(\"sendAnswer [\" + randNum + \"] \");\n        sendTextMessage(senderID, listAnswers[randNum].answerText);\n      });\n    }\n    ```\n    The data is based from the list of possible answers of a [Magic 8 Ball](https://en.wikipedia.org/wiki/Magic_8-Ball)\n    \n    ![](https://github.com/omatt/messenger-chatbot/blob/master/res/sample_db.png \"Magic 8 Ball Answers\")\n    \n11. Create a file named Procfile and copy the line below. This is so Heroku can know what file to run.\n\n    ```\n    web: node index.js\n    ```\n    \n12. Commit all the code with Git then create a new Heroku instance and push the code to the cloud.\n    \n    ```\n    git init\n    git add .\n    git commit --message \"Initial commit\"\n    heroku create  // This will create a new project on Heroku, do this if you don't have a project for your chatbot yet.\n    git push heroku master\n    ```\n\n13. Test your bot\n\n14. Make your bot public! Try this bot now - [Magic X Ball](https://m.me/magicxball)\n    * You may share your bot by sharing its link https://m.me/\u003cPAGE_USERNAME\u003e\n    * If you haven't published your bot yet, it can only respond to registered Developers/Testers on your [Facebook App](https://developers.facebook.com/apps/APP_ID/roles/)\n    * Publish your Facebook App (Messenger Bot) by following their guide [here](https://developers.facebook.com/docs/messenger-platform/product-overview/launch).\n\n## Sources\nThis won't be possible without the sources from:\n* Facebook Messenger Platform guide [link](https://developers.facebook.com/docs/messenger-platform/guides/quick-start)\n* Firebase docs [link](https://firebase.google.com/docs/database/web/start)\n* jw84/messenger-bot-tutorial [link](https://github.com/jw84/messenger-bot-tutorial)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomatt%2Fmessenger-chatbot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomatt%2Fmessenger-chatbot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomatt%2Fmessenger-chatbot/lists"}