Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. function saveContexts(userId, contexts) {
  2. let UID = userId;
  3.  
  4. //get all contexts + parameters
  5.  
  6. if (contexts === undefined) {
  7. console.log("contexts are undefined! returning");
  8. return false;
  9. }
  10. db.collection("user-contexts-prod").doc(UID).set({
  11. dateCreated: new Date(),
  12. contexts: JSON.stringify(contexts)
  13. })
  14. .then(function () {
  15. console.log("success!");
  16. return true;
  17. })
  18. .catch(function (error) {
  19. console.log("error writing document..", error);
  20. return false;
  21. });
  22. }
  23.  
  24. async function getContexts(userId) {
  25. let UID = userId;
  26. let docRef = db.collection("user-contexts-prod").doc(UID);
  27. return docRef.get()
  28. .then(res => {
  29. if (res.exists) {
  30. let contexts = JSON.parse(res.data().contexts);
  31. console.log("<><> parsed contexts <><>: ");
  32. console.log(contexts);
  33. return contexts;
  34. } else {
  35. console.log(" UID DOES NOT EXIST!");
  36. return false;
  37. }
  38. })
  39. }
  40.  
  41. knownUser = await db.isKnownUser(senderId);
  42. if (knownUser) {
  43. //knownUser
  44. console.log("Known user");
  45. let userData = db.getUserDataById(senderId)
  46.  
  47. //initialize contexts with data you need
  48.  
  49. payload = returningUser_useSameData();
  50. messenger.send(payload, senderId);
  51.  
  52. dashbot.logBotMessage(payload.toString, sessionId, intentName);
  53. break;
  54.  
  55. } else {
  56. //newUser
  57. console.log("new user");
  58.  
  59. createContext('await_fillInTogether', '', sessionPath, sessionId, 1);
  60. createContext('session', '', sessionPath, sessionId, 500);
  61.  
  62. payload = fillInTogetherNewUser();
  63. messenger.send(payload, senderId);
  64.  
  65. dashbot.logBotMessage(payload.toString, sessionId, intentName);
  66. break;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement