123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package structs
- import (
- "encoding/json"
- "fmt"
- "regexp"
- "strings"
- tgApi "github.com/go-telegram-bot-api/telegram-bot-api"
- )
- type Vacancy struct {
- FormId string `json:"formId"`
- ResponseId string `json:"responseId"`
- ResponseUrl string `json:"responseUrl"`
- Position string `json:"position"`
- Company string `json:"company"`
- City string `json:"city"`
- Remote string `json:"remote"`
- Salary string `json:"salary"`
- Description string `json:"description"`
- Details string `json:"details"`
- Telegram string `json:"telegram"`
- EditUrl string `json:"editUrl"`
- }
- func NewVacancyFromJson(jsonString []byte) (result *Vacancy) {
- json.Unmarshal(jsonString, &result)
- return
- }
- func (vacancy *Vacancy) Message() (result string, format string) {
- format = "HTML"
- if len(vacancy.Description) > 0 {
- result = fmt.Sprintf(messageTemplateWithDescription(),
- boldText(vacancy.Position),
- boldText(vacancy.Company),
- boldText(vacancy.City),
- boldText(vacancy.Remote),
- boldText(vacancy.Salary),
- replaceLineBreaks(vacancy.Description))
- } else {
- result = fmt.Sprintf(messageTemplateWithoutDescription(),
- boldText(vacancy.Position),
- boldText(vacancy.Company),
- boldText(vacancy.City),
- boldText(vacancy.Remote),
- boldText(vacancy.Salary))
- }
- return
- }
- func (vacancy *Vacancy) FullKeyboard() (result tgApi.InlineKeyboardMarkup) {
- vacancy.vacancyKeyboard(&result)
- vacancy.controlKeyboard(&result)
- return
- }
- func (vacancy *Vacancy) ChannelKeyboard() (result tgApi.InlineKeyboardMarkup) {
- vacancy.vacancyKeyboard(&result)
- return
- }
- func (vacancy *Vacancy) ToJson() []byte {
- jsonString, err := json.Marshal(vacancy)
- if nil != err {
- panic(err)
- }
- return jsonString
- }
- func (vacancy *Vacancy) DbKey() string {
- return fmt.Sprintf("%s/%s", vacancy.FormId, vacancy.ResponseId)
- }
- func (vacancy *Vacancy) vacancyKeyboard(keyboard *tgApi.InlineKeyboardMarkup) {
- if len(vacancy.Telegram) > 0 {
- keyboard.InlineKeyboard = append(keyboard.InlineKeyboard,
- tgApi.NewInlineKeyboardRow(tgApi.NewInlineKeyboardButtonURL("Связаться в Telegram", tgHandleToUrl(vacancy.Telegram))))
- }
- if len(vacancy.Details) > 0 && strings.HasPrefix(vacancy.Details, "http") {
- keyboard.InlineKeyboard = append(keyboard.InlineKeyboard,
- tgApi.NewInlineKeyboardRow(tgApi.NewInlineKeyboardButtonURL("Подробности", vacancy.Details)))
- }
- }
- func (vacancy *Vacancy) controlKeyboard(keyboard *tgApi.InlineKeyboardMarkup) {
- keyboard.InlineKeyboard = append(keyboard.InlineKeyboard,
- tgApi.NewInlineKeyboardRow(tgApi.NewInlineKeyboardButtonData("Confim publishing", fmt.Sprintf("confirm:%s", vacancy.DbKey()))))
- }
- func boldText(original string) (result string) {
- // result = fmt.Sprintf("*%s*", original)
- result = fmt.Sprintf("<b>%s</b>", original)
- return
- }
- func replaceLineBreaks(original string) (result string) {
- re := regexp.MustCompile(`(?i)<br/>`)
- result = re.ReplaceAllString(original, "\n")
- return
- }
- func tgHandleToUrl(original string) (result string) {
- original = strings.TrimSpace(original)
- if strings.HasPrefix(original, "http://") || strings.HasPrefix(original, "https://") {
- result = original
- } else if strings.HasPrefix(original, "@") {
- result = fmt.Sprintf("https://t.me/%s", strings.TrimPrefix(original, "@"))
- } else {
- result = fmt.Sprintf("https://t.me/%s", original)
- }
- return
- }
- func messageTemplateWithDescription() string {
- return `
- %s
- Компания: %s
- Город: %s, %s
- Зп: %s
- Краткое описание:
- %s
- `
- }
- func messageTemplateWithoutDescription() string {
- return `
- %s
- Компания: %s
- Город: %s, %s
- Зп: %s
- `
- }
|