diff --git a/Makefile b/Makefile index 5de013a..ab87c5d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -all: broker +all: clessy-broker -broker: - go build -o clessy-broker ./broker \ No newline at end of file +clessy-broker: + go build -o clessy-broker ./broker diff --git a/broker/action.go b/broker/action.go index b230044..0feac77 100644 --- a/broker/action.go +++ b/broker/action.go @@ -1,4 +1,4 @@ -package broker +package main import ( "../tg" diff --git a/broker/clients.go b/broker/clients.go index 39fba51..d05c4ef 100644 --- a/broker/clients.go +++ b/broker/clients.go @@ -50,7 +50,7 @@ func handleClient(c net.Conn) { } func removeCon(c net.Conn) { - for i, con := range Clients { + for i, con := range clients { if c == con { clients = append(clients[:i], clients[i+1:]...) } diff --git a/broker/main.go b/broker/main.go index 730c316..994957f 100644 --- a/broker/main.go +++ b/broker/main.go @@ -21,7 +21,7 @@ func assert(err error) { } } -var api Telegram +var api *Telegram func main() { cfgpath := flag.String("config", "config.json", "Path to configuration file") @@ -39,7 +39,7 @@ func main() { // Setup webhook handler go func() { - http.HandlerFunc(config.Token, webhook) + http.HandleFunc(config.Token, webhook) err := http.ListenAndServe(config.BindServer, nil) assert(err) }() diff --git a/broker/telegram.go b/broker/telegram.go index 087efba..d1beb5b 100644 --- a/broker/telegram.go +++ b/broker/telegram.go @@ -30,11 +30,11 @@ func (t Telegram) setWebhook(webhook string) { } } -func (t Telegram) apiURL(method string) { +func (t Telegram) apiURL(method string) string { return APIEndpoint + "bot" + t.Token + "/" + method } -func checkerr(method string, err error) { +func checkerr(method string, err error) bool { if err != nil { log.Printf("Received error with call to %s: %s\n", method, err.Error()) return true diff --git a/broker/webhook.go b/broker/webhook.go index 3a1b991..4ecbbf9 100644 --- a/broker/webhook.go +++ b/broker/webhook.go @@ -4,6 +4,8 @@ import ( "encoding/json" "log" "net/http" + + "../tg" ) func webhook(rw http.ResponseWriter, req *http.Request) { diff --git a/tg/api.go b/tg/api.go index 0e01927..0d3f573 100644 --- a/tg/api.go +++ b/tg/api.go @@ -19,94 +19,94 @@ const ( type APIChat struct { UserID int `json:"id"` Type ChatType `json:"type"` - Title string `json:"title,omitempty"` - Username string `json:"username,omitempty"` - FirstName string `json:"first_name,omitempty"` - LastName string `json:"last_name,omitempty"` + Title *string `json:"title,omitempty"` + Username *string `json:"username,omitempty"` + FirstName *string `json:"first_name,omitempty"` + LastName *string `json:"last_name,omitempty"` } type APIMessage struct { MessageID int `json:"message_id"` User APIUser `json:"from"` Time int `json:"date"` - Chat APIChat `json:"chat"` - FwdUser APIUpdate `json:"forward_from,omitempty"` - FwdTime int `json:"forward_date,omitempty"` - ReplyTo APIMessage `json:"reply_to_message,omitempty"` - Text string `json:"text,omitempty"` - Audio APIAudio `json:"audio,omitempty"` - Document APIDocument `json:"document,omitempty"` + Chat *APIChat `json:"chat"` + FwdUser *APIUpdate `json:"forward_from,omitempty"` + FwdTime *int `json:"forward_date,omitempty"` + ReplyTo *APIMessage `json:"reply_to_message,omitempty"` + Text *string `json:"text,omitempty"` + Audio *APIAudio `json:"audio,omitempty"` + Document *APIDocument `json:"document,omitempty"` Photo []APIPhotoSize `json:"photo,omitempty"` - Sticker APISticker `json:"sticker,omitempty"` - Video APIVideo `json:"video,omitempty"` - Voice APIVoice `json:"voice,omitempty"` - Caption string `json:"caption,omitempty"` - Contact APIContact `json:"contact,omitempty"` - Location APILocation `json:"location,omitempty"` - NewUser APIUser `json:"new_chat_partecipant",omitempty"` - LeftUser APIUser `json:"left_chat_partecipant,omitempty"` - PhotoDeleted bool `json:"delete_chat_photo,omitempty"` - GroupCreated bool `json:"group_chat_created,omitempty"` - SupergroupCreated bool `json:"supergroup_chat_created,omitempty"` - ChannelCreated bool `json:"channel_chat_created,omitempty"` - GroupToSuper int `json:"migrate_to_chat_id,omitempty"` - GroupFromSuper int `json:"migrate_from_chat_id,omitempty"` + Sticker *APISticker `json:"sticker,omitempty"` + Video *APIVideo `json:"video,omitempty"` + Voice *APIVoice `json:"voice,omitempty"` + Caption *string `json:"caption,omitempty"` + Contact *APIContact `json:"contact,omitempty"` + Location *APILocation `json:"location,omitempty"` + NewUser *APIUser `json:"new_chat_partecipant",omitempty"` + LeftUser *APIUser `json:"left_chat_partecipant,omitempty"` + PhotoDeleted *bool `json:"delete_chat_photo,omitempty"` + GroupCreated *bool `json:"group_chat_created,omitempty"` + SupergroupCreated *bool `json:"supergroup_chat_created,omitempty"` + ChannelCreated *bool `json:"channel_chat_created,omitempty"` + GroupToSuper *int `json:"migrate_to_chat_id,omitempty"` + GroupFromSuper *int `json:"migrate_from_chat_id,omitempty"` } type APIPhotoSize struct { FileID string `json:"file_id"` Width int `json:"width"` Height int `json:"height"` - FileSize int `json:"file_size,omitempty"` + FileSize *int `json:"file_size,omitempty"` } type APIAudio struct { - FileID string `json:"file_id"` - Duration int `json:"duration"` - Performer string `json:"performer,omitempty"` - Title string `json:"title,omitempty"` - MimeType string `json:"mime_type,omitempty"` - FileSize int `json:"file_size,omitempty"` + FileID string `json:"file_id"` + Duration int `json:"duration"` + Performer *string `json:"performer,omitempty"` + Title *string `json:"title,omitempty"` + MimeType *string `json:"mime_type,omitempty"` + FileSize *int `json:"file_size,omitempty"` } type APIDocument struct { - FileID string `json:"file_id"` - Thumbnail APIPhotoSize `json:"thumb,omitempty"` - Filename string `json:"file_name"` - MimeType string `json:"mime_type,omitempty"` - FileSize int `json:"file_size,omitempty"` + FileID string `json:"file_id"` + Thumbnail *APIPhotoSize `json:"thumb,omitempty"` + Filename string `json:"file_name"` + MimeType *string `json:"mime_type,omitempty"` + FileSize *int `json:"file_size,omitempty"` } type APISticker struct { - FileID string `json:"file_id"` - Width int `json:"width"` - Height int `json:"height"` - Thumbnail APIPhotoSize `json:"thumb,omitempty"` - FileSize int `json:"file_size,omitempty"` + FileID string `json:"file_id"` + Width int `json:"width"` + Height int `json:"height"` + Thumbnail *APIPhotoSize `json:"thumb,omitempty"` + FileSize *int `json:"file_size,omitempty"` } type APIVideo struct { - FileID string `json:"file_id"` - Width int `json:"width"` - Height int `json:"height"` - Duration int `json:"duration"` - Thumbnail APIPhotoSize `json:"thumb,omitempty"` - MimeType string `json:"mime_type,omitempty"` - FileSize int `json:"file_size,omitempty"` + FileID string `json:"file_id"` + Width int `json:"width"` + Height int `json:"height"` + Duration int `json:"duration"` + Thumbnail *APIPhotoSize `json:"thumb,omitempty"` + MimeType *string `json:"mime_type,omitempty"` + FileSize *int `json:"file_size,omitempty"` } type APIVoice struct { - FileID string `json:"file_id"` - Duration int `json:"duration"` - MimeType string `json:"mime_type,omitempty"` - FileSize int `json:"file_size,omitempty"` + FileID string `json:"file_id"` + Duration int `json:"duration"` + MimeType *string `json:"mime_type,omitempty"` + FileSize *int `json:"file_size,omitempty"` } type APIContact struct { - PhoneNumber string `json:"phone_number"` - FirstName string `json:"first_name"` - LastName string `json:"last_name,omitempty"` - UserID int `json:"user_id,omitempty"` + PhoneNumber string `json:"phone_number"` + FirstName string `json:"first_name"` + LastName *string `json:"last_name,omitempty"` + UserID *int `json:"user_id,omitempty"` } type APILocation struct {