Use int64 for IDs, support captions

This commit is contained in:
Hamcha 2016-09-01 14:05:32 +02:00
parent 265938cdd7
commit aa425600e3
6 changed files with 26 additions and 22 deletions

View file

@ -55,12 +55,12 @@ func (t Telegram) SetWebhook(webhook string) {
// SendTextMessage sends an HTML-styled text message to a specified chat // SendTextMessage sends an HTML-styled text message to a specified chat
func (t Telegram) SendTextMessage(data tg.ClientTextMessageData) { func (t Telegram) SendTextMessage(data tg.ClientTextMessageData) {
postdata := url.Values{ postdata := url.Values{
"chat_id": {strconv.Itoa(data.ChatID)}, "chat_id": {strconv.FormatInt(data.ChatID, 10)},
"text": {data.Text}, "text": {data.Text},
"parse_mode": {"HTML"}, "parse_mode": {"HTML"},
} }
if data.ReplyID != nil { if data.ReplyID != nil {
postdata["reply_to_message_id"] = []string{strconv.Itoa(*(data.ReplyID))} postdata["reply_to_message_id"] = []string{strconv.FormatInt(*(data.ReplyID), 10)}
} }
_, err := http.PostForm(t.apiURL("sendMessage"), postdata) _, err := http.PostForm(t.apiURL("sendMessage"), postdata)
@ -86,10 +86,14 @@ func (t Telegram) SendPhoto(data tg.ClientPhotoData) {
part.Write(photobytes[0:decoded]) part.Write(photobytes[0:decoded])
// Write other fields // Write other fields
writer.WriteField("chat_id", strconv.Itoa(data.ChatID)) writer.WriteField("chat_id", strconv.FormatInt(data.ChatID, 10))
if data.ReplyID != nil { if data.ReplyID != nil {
writer.WriteField("reply_to_message_id", strconv.Itoa(*data.ReplyID)) writer.WriteField("reply_to_message_id", strconv.FormatInt(*data.ReplyID, 10))
}
if data.Caption != "" {
writer.WriteField("caption", data.Caption)
} }
err = writer.Close() err = writer.Close()

View file

@ -174,7 +174,7 @@ func memegen(broker *tg.Broker, update tg.APIMessage) {
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID) broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID)
return return
} }
broker.SendPhoto(update.Chat, buf.Bytes(), "meme.jpg", nil, &update.MessageID) broker.SendPhoto(update.Chat, buf.Bytes(), "meme.jpg", "", &update.MessageID)
}) })
} }
} }

View file

@ -14,7 +14,7 @@ func assert(err error) {
} }
var db *bolt.DB var db *bolt.DB
var chatID *int var chatID *int64
func process(broker *tg.Broker, update tg.APIMessage) { func process(broker *tg.Broker, update tg.APIMessage) {
// Process messages from marked chat only // Process messages from marked chat only
@ -29,7 +29,7 @@ func main() {
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port") brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
webBind := flag.String("webserver", "localhost:7315", "Address to bind webserver to") webBind := flag.String("webserver", "localhost:7315", "Address to bind webserver to")
boltdbFile := flag.String("boltdb", "stats.db", "BoltDB database file") boltdbFile := flag.String("boltdb", "stats.db", "BoltDB database file")
chatID = flag.Int("chatid", -14625256, "Telegram Chat ID to count stats for") chatID = flag.Int64("chatid", -14625256, "Telegram Chat ID to count stats for")
flag.Parse() flag.Parse()
var err error var err error

View file

@ -2,7 +2,7 @@ package tg
// APIUser represents the "User" JSON structure // APIUser represents the "User" JSON structure
type APIUser struct { type APIUser struct {
UserID int `json:"id"` UserID int64 `json:"id"`
FirstName string `json:"first_name"` FirstName string `json:"first_name"`
LastName string `json:"last_name,omitempty"` LastName string `json:"last_name,omitempty"`
Username string `json:"username,omitempty"` Username string `json:"username,omitempty"`
@ -27,7 +27,7 @@ const (
// APIChat represents the "Chat" JSON structure // APIChat represents the "Chat" JSON structure
type APIChat struct { type APIChat struct {
ChatID int `json:"id"` ChatID int64 `json:"id"`
Type ChatType `json:"type"` Type ChatType `json:"type"`
Title *string `json:"title,omitempty"` Title *string `json:"title,omitempty"`
Username *string `json:"username,omitempty"` Username *string `json:"username,omitempty"`
@ -37,9 +37,9 @@ type APIChat struct {
// APIMessage represents the "Message" JSON structure // APIMessage represents the "Message" JSON structure
type APIMessage struct { type APIMessage struct {
MessageID int `json:"message_id"` MessageID int64 `json:"message_id"`
User APIUser `json:"from"` User APIUser `json:"from"`
Time int `json:"date"` Time int64 `json:"date"`
Chat *APIChat `json:"chat"` Chat *APIChat `json:"chat"`
FwdUser *APIUpdate `json:"forward_from,omitempty"` FwdUser *APIUpdate `json:"forward_from,omitempty"`
FwdTime *int `json:"forward_date,omitempty"` FwdTime *int `json:"forward_date,omitempty"`
@ -60,8 +60,8 @@ type APIMessage struct {
GroupCreated *bool `json:"group_chat_created,omitempty"` GroupCreated *bool `json:"group_chat_created,omitempty"`
SupergroupCreated *bool `json:"supergroup_chat_created,omitempty"` SupergroupCreated *bool `json:"supergroup_chat_created,omitempty"`
ChannelCreated *bool `json:"channel_chat_created,omitempty"` ChannelCreated *bool `json:"channel_chat_created,omitempty"`
GroupToSuper *int `json:"migrate_to_chat_id,omitempty"` GroupToSuper *int64 `json:"migrate_to_chat_id,omitempty"`
GroupFromSuper *int `json:"migrate_from_chat_id,omitempty"` GroupFromSuper *int64 `json:"migrate_from_chat_id,omitempty"`
} }
// APIPhotoSize represents the "PhotoSize" JSON structure // APIPhotoSize represents the "PhotoSize" JSON structure
@ -124,7 +124,7 @@ type APIContact struct {
PhoneNumber string `json:"phone_number"` PhoneNumber string `json:"phone_number"`
FirstName string `json:"first_name"` FirstName string `json:"first_name"`
LastName *string `json:"last_name,omitempty"` LastName *string `json:"last_name,omitempty"`
UserID *int `json:"user_id,omitempty"` UserID *int64 `json:"user_id,omitempty"`
} }
// APILocation represents the "Location" JSON structure // APILocation represents the "Location" JSON structure
@ -135,7 +135,7 @@ type APILocation struct {
// APIUpdate represents the "Update" JSON structure // APIUpdate represents the "Update" JSON structure
type APIUpdate struct { type APIUpdate struct {
UpdateID int `json:"update_id"` UpdateID int64 `json:"update_id"`
Message APIMessage `json:"message"` Message APIMessage `json:"message"`
} }

View file

@ -37,7 +37,7 @@ func (b *Broker) Close() {
// SendTextMessage sends a HTML-styles text message to a chat. // SendTextMessage sends a HTML-styles text message to a chat.
// A reply_to message ID can be specified as optional parameter. // A reply_to message ID can be specified as optional parameter.
func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int) { func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int64) {
b.sendCmd(ClientCommand{ b.sendCmd(ClientCommand{
Type: CmdSendTextMessage, Type: CmdSendTextMessage,
TextMessageData: &ClientTextMessageData{ TextMessageData: &ClientTextMessageData{
@ -50,7 +50,7 @@ func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int) {
// SendPhoto sends a photo with an optional caption to a chat. // SendPhoto sends a photo with an optional caption to a chat.
// A reply_to message ID can be specified as optional parameter. // A reply_to message ID can be specified as optional parameter.
func (b *Broker) SendPhoto(chat *APIChat, data []byte, filename string, caption *string, original *int) { func (b *Broker) SendPhoto(chat *APIChat, data []byte, filename string, caption string, original *int64) {
b.sendCmd(ClientCommand{ b.sendCmd(ClientCommand{
Type: CmdSendPhoto, Type: CmdSendPhoto,
PhotoData: &ClientPhotoData{ PhotoData: &ClientPhotoData{

View file

@ -39,18 +39,18 @@ const (
// ClientTextMessageData is the required data for a CmdSendTextMessage request // ClientTextMessageData is the required data for a CmdSendTextMessage request
type ClientTextMessageData struct { type ClientTextMessageData struct {
ChatID int ChatID int64
Text string Text string
ReplyID *int `json:",omitempty"` ReplyID *int64 `json:",omitempty"`
} }
// ClientPhotoData is the required data for a CmdSendPhoto request // ClientPhotoData is the required data for a CmdSendPhoto request
type ClientPhotoData struct { type ClientPhotoData struct {
ChatID int ChatID int64
Bytes string Bytes string
Filename string Filename string
Caption *string `json:",omitempty"` Caption string `json:",omitempty"`
ReplyID *int `json:",omitempty"` ReplyID *int64 `json:",omitempty"`
} }
// FileRequestData is the required data for a CmdGetFile request // FileRequestData is the required data for a CmdGetFile request