Vacancy.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package structs
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "regexp"
  6. "strings"
  7. tgApi "github.com/go-telegram-bot-api/telegram-bot-api"
  8. )
  9. type Vacancy struct {
  10. FormId string `json:"formId"`
  11. ResponseId string `json:"responseId"`
  12. ResponseUrl string `json:"responseUrl"`
  13. Position string `json:"position"`
  14. Company string `json:"company"`
  15. City string `json:"city"`
  16. Remote string `json:"remote"`
  17. Salary string `json:"salary"`
  18. Description string `json:"description"`
  19. Details string `json:"details"`
  20. Telegram string `json:"telegram"`
  21. EditUrl string `json:"editUrl"`
  22. }
  23. func NewVacancyFromJson(jsonString []byte) (result *Vacancy) {
  24. json.Unmarshal(jsonString, &result)
  25. return
  26. }
  27. func (vacancy *Vacancy) Message() (result string, format string) {
  28. format = "HTML"
  29. if len(vacancy.Description) > 0 {
  30. result = fmt.Sprintf(messageTemplateWithDescription(),
  31. boldText(vacancy.Position),
  32. boldText(vacancy.Company),
  33. boldText(vacancy.City),
  34. boldText(vacancy.Remote),
  35. boldText(vacancy.Salary),
  36. replaceLineBreaks(vacancy.Description))
  37. } else {
  38. result = fmt.Sprintf(messageTemplateWithoutDescription(),
  39. boldText(vacancy.Position),
  40. boldText(vacancy.Company),
  41. boldText(vacancy.City),
  42. boldText(vacancy.Remote),
  43. boldText(vacancy.Salary))
  44. }
  45. return
  46. }
  47. func (vacancy *Vacancy) FullKeyboard() (result tgApi.InlineKeyboardMarkup) {
  48. vacancy.vacancyKeyboard(&result)
  49. vacancy.controlKeyboard(&result)
  50. return
  51. }
  52. func (vacancy *Vacancy) ChannelKeyboard() (result tgApi.InlineKeyboardMarkup) {
  53. vacancy.vacancyKeyboard(&result)
  54. return
  55. }
  56. func (vacancy *Vacancy) ToJson() []byte {
  57. jsonString, err := json.Marshal(vacancy)
  58. if nil != err {
  59. panic(err)
  60. }
  61. return jsonString
  62. }
  63. func (vacancy *Vacancy) DbKey() string {
  64. return fmt.Sprintf("%s/%s", vacancy.FormId, vacancy.ResponseId)
  65. }
  66. func (vacancy *Vacancy) vacancyKeyboard(keyboard *tgApi.InlineKeyboardMarkup) {
  67. if len(vacancy.Telegram) > 0 {
  68. keyboard.InlineKeyboard = append(keyboard.InlineKeyboard,
  69. tgApi.NewInlineKeyboardRow(tgApi.NewInlineKeyboardButtonURL("Связаться в Telegram", tgHandleToUrl(vacancy.Telegram))))
  70. }
  71. if len(vacancy.Details) > 0 && strings.HasPrefix(vacancy.Details, "http") {
  72. keyboard.InlineKeyboard = append(keyboard.InlineKeyboard,
  73. tgApi.NewInlineKeyboardRow(tgApi.NewInlineKeyboardButtonURL("Подробности", vacancy.Details)))
  74. }
  75. }
  76. func (vacancy *Vacancy) controlKeyboard(keyboard *tgApi.InlineKeyboardMarkup) {
  77. keyboard.InlineKeyboard = append(keyboard.InlineKeyboard,
  78. tgApi.NewInlineKeyboardRow(tgApi.NewInlineKeyboardButtonData("Confim publishing", fmt.Sprintf("confirm:%s", vacancy.DbKey()))))
  79. }
  80. func boldText(original string) (result string) {
  81. // result = fmt.Sprintf("*%s*", original)
  82. result = fmt.Sprintf("<b>%s</b>", original)
  83. return
  84. }
  85. func replaceLineBreaks(original string) (result string) {
  86. re := regexp.MustCompile(`(?i)<br/>`)
  87. result = re.ReplaceAllString(original, "\n")
  88. return
  89. }
  90. func tgHandleToUrl(original string) (result string) {
  91. original = strings.TrimSpace(original)
  92. if strings.HasPrefix(original, "http://") || strings.HasPrefix(original, "https://") {
  93. result = original
  94. } else if strings.HasPrefix(original, "@") {
  95. result = fmt.Sprintf("https://t.me/%s", strings.TrimPrefix(original, "@"))
  96. } else {
  97. result = fmt.Sprintf("https://t.me/%s", original)
  98. }
  99. return
  100. }
  101. func messageTemplateWithDescription() string {
  102. return `
  103. %s
  104. Компания: %s
  105. Город: %s, %s
  106. Зп: %s
  107. Краткое описание:
  108. %s
  109. `
  110. }
  111. func messageTemplateWithoutDescription() string {
  112. return `
  113. %s
  114. Компания: %s
  115. Город: %s, %s
  116. Зп: %s
  117. `
  118. }