예제 #1
0
func mustBeAuthed(c *integram.Context) (bool, error) {

	provider := c.OAuthProvider()

	if !provider.IsSetup() {
		return false, c.NewMessage().SetText(fmt.Sprintf("To be able to use interactive replies in Telegram, first you need to add oauth application on your hosted Bitbucket instance (admin priveleges required): %s\nAdd application with any name(f.e. Telegram) and specify this *Redirect URI*: \n%s\n\nAfter you press *Submit* you will receive app info. First, send me the *Application Id*", c.ServiceBaseURL.String()+"/account/", provider.RedirectURL())).
			SetChat(c.User.ID).
			SetBackupChat(c.Chat.ID).
			EnableHTML().
			EnableForceReply().
			DisableWebPreview().
			SetReplyAction(hostedAppIDEntered, c.ServiceBaseURL.String()).Send()

	}
	if !c.User.OAuthValid() {
		if c.Callback != nil {
			c.AnswerCallbackQuery("You need to authorize me\nUse the \"Tap me to auth\" button", true)
		}
		if !c.User.IsPrivateStarted() && c.Callback != nil {
			kb := c.Callback.Message.InlineKeyboardMarkup
			kb.AddPMSwitchButton(c.Bot(), "👉  Tap me to auth", "auth")
			c.EditPressedInlineKeyboard(kb)
		} else {
			return false, c.NewMessage().SetTextFmt("You need to authorize me to use interactive replies: %s", c.User.OauthInitURL()).
				DisableWebPreview().
				SetChat(c.User.ID).SetBackupChat(c.Chat.ID).Send()
		}
	}

	return true, nil

}
예제 #2
0
func settingsKeyboardPressed(c *integram.Context) error {
	cs := chatSettings(c)

	state := c.Callback.Message.InlineKeyboardMarkup.State

	if state == "categories" {
		state = c.Callback.Data
	}

	btns := integram.InlineButtons{}

	if c.Callback.Data == "back" {
		btns.Append("ci", "CI")
		btns.Append("mr", "Merge requests")
		state = "categories"
	} else if state == "mr" {
		btns.Append("back", "← Back")
		switch c.Callback.Data {
		case "open":
			cs.MR.Open = !stateToBool(c.Callback.State)
		case "close":
			cs.MR.Close = !stateToBool(c.Callback.State)
		case "update":
			cs.MR.Update = !stateToBool(c.Callback.State)
		case "merge":
			cs.MR.Merge = !stateToBool(c.Callback.State)
		}

		c.Chat.SaveSettings(cs)

		btns.AppendWithState(boolToState(cs.MR.Open), "open", boolToMark(cs.MR.Open)+"Open")
		btns.AppendWithState(boolToState(cs.MR.Update), "update", boolToMark(cs.MR.Update)+"Update")
		btns.AppendWithState(boolToState(cs.MR.Merge), "merge", boolToMark(cs.MR.Merge)+"Merge")
		btns.AppendWithState(boolToState(cs.MR.Close), "close", boolToMark(cs.MR.Close)+"Close")

	} else if state == "ci" {
		btns.Append("back", "← Back")
		switch c.Callback.Data {
		case "success":
			cs.CI.Success = !stateToBool(c.Callback.State)
		case "fail":
			cs.CI.Fail = !stateToBool(c.Callback.State)
		case "cancel":
			cs.CI.Cancel = !stateToBool(c.Callback.State)
		}

		c.Chat.SaveSettings(cs)

		btns.AppendWithState(boolToState(cs.CI.Success), "success", boolToMark(cs.CI.Success)+"Success")
		btns.AppendWithState(boolToState(cs.CI.Fail), "fail", boolToMark(cs.CI.Fail)+"Fail")
		btns.AppendWithState(boolToState(cs.CI.Cancel), "cancel", boolToMark(cs.CI.Cancel)+"Cancel")
	}
	return c.EditPressedInlineKeyboard(btns.Markup(1, state))
}
예제 #3
0
func prInlineButtonPressed(c *integram.Context, fullRepoName string, prID int) error {
	if ok, err := mustBeAuthed(c); !ok {
		return err
	}
	pr := api.PullRequest{}

	c.ServiceCache(issueUniqueID(fullRepoName, prID), &pr)

	switch c.Callback.Data {
	case "back":
		return c.EditPressedInlineKeyboard(prInlineKeyboard(&pr))
	case "assign":
		//rest.Repositories.ListPublic()
	}
	return nil

}
예제 #4
0
func issueInlineButtonPressed(c *integram.Context, fullRepoName string, issueID int) error {

	if ok, err := mustBeAuthed(c); !ok {
		return err
	}

	issue := api.Issue{}

	c.ServiceCache(issueUniqueID(fullRepoName, issueID), &issue)
	rest := client(c)

	fmt.Printf("issueID: %d\n", issueID)
	owner, repo := splitRepo(fullRepoName)

	if c.Callback.Message.InlineKeyboardMarkup.State == "status" {
		state := c.Callback.Data
		if state != "back" {
			err := rest.Repositories.Issues.SetState(&api.IssuesOptions{Owner: owner, Repo_slug: repo, Issue_id: issueID, State: state})
			if err != nil {
				return err
			}
			issue.State = state
			storeIssue(c, &issue)

			c.Callback.Data = "back"
		}
	}
	switch c.Callback.Data {
	case "back":
		return c.EditPressedInlineKeyboard(issueInlineKeyboard(&issue))
	/*case "assign":
	case "status":
		buttons := integram.InlineButtons{}
		for b, _ := range issueStates {
			if !strings.EqualFold(b, issue.State) {
				buttons.Append(b, strings.ToUpper(b[0:1])+b[1:])
			}
		}
		buttons.Append("back", "← Back")

		return c.EditPressedInlineKeyboard(buttons.Markup(1, "status"))
	*/
	case "vote":
		me, err := me(rest, c)

		if err != nil {
			return err
		}

		if integram.SliceContainsString(issue.VotedActorsUUID, me.UUID) {
			err = rest.Repositories.Issues.Unvote(&api.IssuesOptions{Owner: owner, Repo_slug: repo, Issue_id: issueID})
			if err != nil {
				return err
			}
			b := issue.VotedActorsUUID[:0]
			for _, x := range issue.VotedActorsUUID {
				if x != me.UUID {
					b = append(b, x)
				}
			}
			issue.VotedActorsUUID = b
			issue.Votes--

		} else {
			err = rest.Repositories.Issues.Vote(&api.IssuesOptions{Owner: owner, Repo_slug: repo, Issue_id: issueID})
			if err != nil {
				return err
			}
			issue.Votes++
			issue.VotedActorsUUID = append(issue.VotedActorsUUID, me.UUID)
		}
		storeIssue(c, &issue)

		kb := issueInlineKeyboard(&issue)
		err = c.EditPressedInlineKeyboard(kb)

		if err != nil {
			return err
		}
	}

	return nil

}