@ -26,7 +26,6 @@ type BotAPI struct {
Self User ` json:"-" `
Self User ` json:"-" `
Client * http . Client ` json:"-" `
Client * http . Client ` json:"-" `
shutdownChannel chan interface { }
}
}
// NewBotAPI creates a new BotAPI instance.
// NewBotAPI creates a new BotAPI instance.
@ -45,7 +44,6 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) {
Token : token ,
Token : token ,
Client : client ,
Client : client ,
Buffer : 100 ,
Buffer : 100 ,
shutdownChannel : make ( chan interface { } ) ,
}
}
self , err := bot . GetMe ( )
self , err := bot . GetMe ( )
@ -380,11 +378,12 @@ func (bot *BotAPI) GetWebhookInfo() (WebhookInfo, error) {
// GetUpdatesChan starts and returns a channel for getting updates.
// GetUpdatesChan starts and returns a channel for getting updates.
func ( bot * BotAPI ) GetUpdatesChan ( config UpdateConfig ) UpdatesChannel {
func ( bot * BotAPI ) GetUpdatesChan ( config UpdateConfig ) UpdatesChannel {
ch := make ( chan Update , bot . Buffer )
ch := make ( chan Update , bot . Buffer )
done := make ( chan struct { } )
go func ( ) {
go func ( ) {
for {
for {
select {
select {
case <- bot . shut downChan nel :
case <- done :
return
return
default :
default :
}
}
@ -401,28 +400,37 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) UpdatesChannel {
for _ , update := range updates {
for _ , update := range updates {
if update . UpdateID >= config . Offset {
if update . UpdateID >= config . Offset {
config . Offset = update . UpdateID + 1
config . Offset = update . UpdateID + 1
ch <- update
select {
case ch <- update :
case <- done :
return
}
}
}
}
}
}
}
} ( )
} ( )
return ch
updatesCh := UpdatesChannel {
channel : ch ,
done : done ,
}
}
// StopReceivingUpdates stops the go routine which receives updates
return updatesCh
func ( bot * BotAPI ) StopReceivingUpdates ( ) {
if bot . Debug {
log . Println ( "Stopping the update receiver routine..." )
}
close ( bot . shutdownChannel )
}
}
// ListenForWebhook registers a http handler for a webhook.
// ListenForWebhook registers a http handler for a webhook.
func ( bot * BotAPI ) ListenForWebhook ( pattern string ) UpdatesChannel {
func ( bot * BotAPI ) ListenForWebhook ( pattern string ) UpdatesChannel {
ch := make ( chan Update , bot . Buffer )
ch := make ( chan Update , bot . Buffer )
done := make ( chan struct { } )
http . HandleFunc ( pattern , func ( w http . ResponseWriter , r * http . Request ) {
http . HandleFunc ( pattern , func ( w http . ResponseWriter , r * http . Request ) {
select {
case <- done :
return
default :
}
bytes , _ := ioutil . ReadAll ( r . Body )
bytes , _ := ioutil . ReadAll ( r . Body )
var update Update
var update Update
@ -431,7 +439,12 @@ func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel {
ch <- update
ch <- update
} )
} )
return ch
updatesCh := UpdatesChannel {
channel : ch ,
done : done ,
}
return updatesCh
}
}
// GetChat gets information about a chat.
// GetChat gets information about a chat.