github.com/matthieudolci/hatcher@v0.2.8/bot/handler.go (about)

     1  package bot
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"strings"
     8  
     9  	"github.com/matthieudolci/hatcher/common"
    10  
    11  	log "github.com/Sirupsen/logrus"
    12  	"github.com/julienschmidt/httprouter"
    13  	"github.com/matthieudolci/hatcher/scheduler"
    14  	"github.com/matthieudolci/hatcher/setup"
    15  	"github.com/slack-go/slack"
    16  )
    17  
    18  const isManagerYes = "isManagerYes"
    19  
    20  // SlackPostHandler listen on /slack for answer from the questions asked in setup.go
    21  // and dispatch to the good functions
    22  func SlackPostHandler(s *common.Slack) func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    23  	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    24  		if r.URL.Path != "/slack" {
    25  			w.WriteHeader(http.StatusNotFound)
    26  			_, err := w.Write([]byte(fmt.Sprintf("incorrect path: %s", r.URL.Path)))
    27  			if err != nil {
    28  				log.WithError(err).Error("Could not post the message incorrect path")
    29  			}
    30  			log.WithFields(log.Fields{
    31  				"urlpath": r.URL.Path,
    32  			}).Error("incorrect path")
    33  			return
    34  		}
    35  
    36  		if r.Body == nil {
    37  			w.WriteHeader(http.StatusNotAcceptable)
    38  			_, err := w.Write([]byte("empty body"))
    39  			if err != nil {
    40  				log.WithError(err).Error("empty body")
    41  			}
    42  			return
    43  		}
    44  		defer r.Body.Close()
    45  
    46  		err := r.ParseForm()
    47  		if err != nil {
    48  			w.WriteHeader(http.StatusGone)
    49  			_, err = w.Write([]byte("could not parse body"))
    50  			if err != nil {
    51  				log.WithError(err).Error("could not parse body")
    52  			}
    53  			return
    54  		}
    55  
    56  		// slack API calls the data POST a 'payload'
    57  		reply := r.PostFormValue("payload")
    58  		if len(reply) == 0 {
    59  			w.WriteHeader(http.StatusNoContent)
    60  			_, err = w.Write([]byte("could not find payload"))
    61  			if err != nil {
    62  				log.WithError(err).Error("Could not find payload")
    63  			}
    64  			return
    65  		}
    66  
    67  		var payload slack.InteractionCallback
    68  
    69  		err = json.NewDecoder(strings.NewReader(reply)).Decode(&payload)
    70  		if err != nil {
    71  			w.WriteHeader(http.StatusGone)
    72  			_, err = w.Write([]byte("could not process payload"))
    73  			if err != nil {
    74  				log.WithError(err).Error("Could not process payload")
    75  			}
    76  			return
    77  		}
    78  
    79  		value := payload.ActionCallback.AttachmentActions[0].Value
    80  		name := payload.ActionCallback.AttachmentActions[0].Name
    81  		api := slack.New(s.Token)
    82  		userid := fmt.Sprintf(payload.User.ID)
    83  
    84  		user, err := api.GetUserInfo(userid)
    85  		if err != nil {
    86  			log.WithError(err).Error("Could not get user info")
    87  			return
    88  		}
    89  
    90  		fullname := fmt.Sprintf(user.Profile.RealName)
    91  		displayname := fmt.Sprintf(user.Profile.DisplayName)
    92  		email := fmt.Sprintf(user.Profile.Email)
    93  		channelid := fmt.Sprintf(payload.Channel.ID)
    94  
    95  		switch value {
    96  		case "SetupYes":
    97  			_, err := w.Write([]byte(":white_check_mark: - Starting the setup of your user."))
    98  			if err != nil {
    99  				log.WithError(err).Error("Could not post the message Starting the setup of your user")
   100  			}
   101  
   102  			err = setup.InitBot(userid, email, fullname, displayname)
   103  			if err != nil {
   104  				log.WithError(err).Error("Could not start initBot for value SetupYes")
   105  			}
   106  			log.Info("Started initBot for value SetupYes")
   107  
   108  			err = setup.AskWhoIsManager(s, channelid, userid)
   109  			if err != nil {
   110  				log.WithError(err).Error("Could not start askWhoIsManager")
   111  			}
   112  			log.Info("Start askWhoIsManager")
   113  
   114  		case "SetupNo":
   115  			_, err := w.Write([]byte("No worries, let me know if you want to later on!"))
   116  			if err != nil {
   117  				log.WithError(err).Error("Could not post the message No worries, let me know if you want to later on")
   118  			}
   119  
   120  		case "RemoveYes":
   121  			err = setup.RemoveBot(userid, fullname)
   122  			if err != nil {
   123  				log.WithError(err).Error("Could not start removeBot")
   124  			}
   125  			log.Info("Started removeBot")
   126  
   127  			err := scheduler.GetTimeAndUsersForScheduler(s)
   128  			if err != nil {
   129  				log.WithError(err).Error("Could not start GetTimeAndUsersForScheduler for value RemoveYes")
   130  			}
   131  			log.Info("Started GetTimeAndUsersForScheduler for value RemoveYes")
   132  
   133  			_, err = w.Write([]byte("Sorry to see you go. Your user was deleted."))
   134  			if err != nil {
   135  				log.WithError(err).Error("Could not post the message Sorry to see you go")
   136  			}
   137  
   138  		case "RemoveNo":
   139  			_, err := w.Write([]byte("Glad you decided to stay :smiley:"))
   140  			if err != nil {
   141  				log.WithError(err).Error("Could not post the message Glad you decided to stay")
   142  			}
   143  
   144  		case isManagerYes:
   145  			answer := fmt.Sprintf(payload.ActionCallback.AttachmentActions[0].Value)
   146  
   147  			if answer == isManagerYes {
   148  				value := fmt.Sprintf("true")
   149  
   150  				err := setup.IsManager(userid, fullname, value)
   151  				if err != nil {
   152  					log.WithError(err).Error("Could not start IsManager for value isManagerYes")
   153  				}
   154  				log.Info("Start IsManager for value isManagerYes")
   155  
   156  				_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - You are setup as a manager.")))
   157  				if err != nil {
   158  					log.WithError(err).Error("Could not post You are setup as a manager message")
   159  				}
   160  			}
   161  
   162  		case "isManagerNo":
   163  			answer := fmt.Sprintf(payload.ActionCallback.AttachmentActions[0].Value)
   164  
   165  			if answer == "isManagerNo" {
   166  				value := fmt.Sprintf("false")
   167  
   168  				err := setup.IsManager(userid, fullname, value)
   169  				if err != nil {
   170  					log.WithError(err).Error("Could not start IsManager for value isManagerNo")
   171  				}
   172  				log.Info("Start IsManager for value isManagerNo")
   173  
   174  				_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - %s your user is now setup!\n", displayname)))
   175  				if err != nil {
   176  					log.WithError(err).Error("Could not post the final message of the setup for none manager user")
   177  				}
   178  			}
   179  		}
   180  
   181  		switch name {
   182  		case "ManagerChosen":
   183  			managerid := fmt.Sprintf(payload.ActionCallback.AttachmentActions[0].SelectedOptions[0].Value)
   184  
   185  			manager, err := api.GetUserInfo(managerid)
   186  			if err != nil {
   187  				log.WithError(err).Error("Could not start GetUserInfo for name ManagerChosen")
   188  			}
   189  			log.Info("Getting user informations for name ManagerChosen")
   190  
   191  			managername := fmt.Sprintf(manager.RealName)
   192  
   193  			err = setup.InitManager(userid, fullname, managerid, managername)
   194  			if err != nil {
   195  				log.WithError(err).Error("Could not start initManager for name ManagerChosen")
   196  			}
   197  			log.Info("Started initManager for name ManagerChosen")
   198  
   199  			_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - %s was setup as your manager.", managername)))
   200  			if err != nil {
   201  				log.WithError(err).Error("Could not post the you are setup as a manager")
   202  			}
   203  
   204  			timestandup := setup.GetStandupTimeFromManager(managerid)
   205  			channel := setup.GetStandupChannelFromManager(managerid)
   206  
   207  			if len(timestandup) > 0 {
   208  				err = setup.UpdateTimeStandup(managerid, userid, timestandup)
   209  				if err != nil {
   210  					log.WithError(err).Error("Could not start updateTimeStandup for name ManagerChosen")
   211  				}
   212  				log.Info("Started updateTimeStandup for name ManagerChosen")
   213  			} else {
   214  				err = setup.UpdateTimeStandup(managerid, userid, "NULL")
   215  				if err != nil {
   216  					log.WithError(err).Error("Could not start updateTimeStandup for name ManagerChosen")
   217  				}
   218  				log.Info("Started updateTimeStandup for name ManagerChosen")
   219  			}
   220  
   221  			err = setup.UpdateChannelStandup(managerid, userid, channel)
   222  			if err != nil {
   223  				log.WithError(err).Error("Could not start updateTimeStandup for name ManagerChosen")
   224  			}
   225  			log.Info("Started updateTimeStandup for name ManagerChosen")
   226  
   227  			err = scheduler.GetTimeAndUsersForScheduler(s)
   228  			if err != nil {
   229  				log.WithError(err).Error("Could not start GetTimeAndUsersForScheduler for name ChannelStandupChosen")
   230  			}
   231  			log.Info("Start GetTimeAndUsersForScheduler for name ChannelStandupChosen")
   232  
   233  			err = setup.AskIfManager(s, channelid, userid)
   234  			if err != nil {
   235  				log.WithError(err).Error("Could not start askIfManager for name ManagerChosen")
   236  			}
   237  			log.Info("Started askIfManager for name ManagerChosen")
   238  
   239  		case "NoManagerChosen":
   240  			_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - No manager selected")))
   241  			if err != nil {
   242  				log.WithError(err).Error("Could not post the you are setup as a manager")
   243  			}
   244  
   245  			err = setup.AskIfManager(s, channelid, userid)
   246  			if err != nil {
   247  				log.WithError(err).Error("Could not start askIfManager for name NoManagerChosen")
   248  			}
   249  			log.Info("Started askIfManager for name NoManagerChosen")
   250  
   251  		case isManagerYes:
   252  			err = setup.AskTimeStandup(s, channelid, userid)
   253  			if err != nil {
   254  				log.WithError(err).Error("Could not start askTimeStandup")
   255  			}
   256  			log.Info("Start askTimeStandup")
   257  
   258  		case "StandupTime":
   259  			time := fmt.Sprintf(payload.ActionCallback.AttachmentActions[0].SelectedOptions[0].Value)
   260  
   261  			err := setup.InsertTimeStandup(userid, fullname, time)
   262  			if err != nil {
   263  				log.WithError(err).Error("Could not start insertTimeStandup for name StandupTime")
   264  			}
   265  			log.Info("Start insertTimeStandup for name StandupTime")
   266  
   267  			err = setup.AskWhichChannelStandup(s, channelid, userid)
   268  			if err != nil {
   269  				log.WithError(err).Error("Could not start askWhichChannelStandup for name StandupTime")
   270  			}
   271  			log.Info("Start askWhichChannelStandup for name StandupTime")
   272  
   273  			_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - Team standup setup")))
   274  			if err != nil {
   275  				log.WithError(err).Error("Could not post the message Team standup setup")
   276  			}
   277  
   278  		case "ChannelStandupChosen":
   279  			channel := fmt.Sprintf(payload.ActionCallback.AttachmentActions[0].SelectedOptions[0].Value)
   280  
   281  			err := setup.InsertChannelStandup(userid, fullname, channel)
   282  			if err != nil {
   283  				log.WithError(err).Error("Could not start insertChannelStandup for name ChannelStandupChosen")
   284  			}
   285  			log.Info("Start insertChannelStandup for name ChannelStandupChosen")
   286  
   287  			err = scheduler.GetTimeAndUsersForScheduler(s)
   288  			if err != nil {
   289  				log.WithError(err).Error("Could not start GetTimeAndUsersForScheduler for name ChannelStandupChosen")
   290  			}
   291  			log.Info("Start GetTimeAndUsersForScheduler for name ChannelStandupChosen")
   292  
   293  			_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - %s your user is now setup!", displayname)))
   294  			if err != nil {
   295  				log.WithError(err).Error("Could not post the message Your user is now setup")
   296  			}
   297  		}
   298  		w.WriteHeader(http.StatusOK)
   299  	}
   300  }