Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Commit

Permalink
Add non-blocking communication with telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyaglow committed Jul 2, 2018
1 parent 16828d7 commit 94b9024
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
12 changes: 6 additions & 6 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func (c *Client) sendReport(r *cortex.Report, callback *tgbotapi.CallbackQuery)
attachment := tgbotapi.NewDocumentUpload(callback.Message.Chat.ID, fb)
attachment.ReplyToMessageID = callback.Message.ReplyToMessage.MessageID
attachment.Caption = buildTaxonomies(r.Taxonomies())
_, err := c.Bot.Send(attachment)
go c.Bot.Send(attachment)

return err
return nil
}

// processCallback analyzes observables with a selected set of analyzers
Expand Down Expand Up @@ -77,14 +77,14 @@ func (c *Client) processCallback(callback *tgbotapi.CallbackQuery) error {
case "close":
kb := showButton()
edit := tgbotapi.NewEditMessageReplyMarkup(callback.Message.Chat.ID, callback.Message.MessageID, *kb)
c.Bot.Send(edit)
go c.Bot.Send(edit)
case "show":
kb, err := c.analyzersButtons(dataType(callback.Message.ReplyToMessage.Text))
if err != nil {
return err
}
edit := tgbotapi.NewEditMessageReplyMarkup(callback.Message.Chat.ID, callback.Message.MessageID, *kb)
c.Bot.Send(edit)
go c.Bot.Send(edit)
default:
r, err := c.Cortex.Analyzers.Run(context.Background(), callback.Data, j, time.Minute*5)
if err != nil {
Expand All @@ -100,9 +100,9 @@ func (c *Client) processCallback(callback *tgbotapi.CallbackQuery) error {
}

cbcfg := tgbotapi.NewCallback(callback.ID, "")
_, err = c.Bot.AnswerCallbackQuery(cbcfg)
go c.Bot.AnswerCallbackQuery(cbcfg)

return err
return nil
}

// analyzersButtons returns a markup of analyzers as buttons
Expand Down
16 changes: 9 additions & 7 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ func (c *Client) Run() {

if update.CallbackQuery != nil {
log.Printf("[%s] %s", update.CallbackQuery.Message.From.UserName, update.CallbackQuery.Message.Text)
if err := c.processCallback(update.CallbackQuery); err != nil {
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, "")
msg.ReplyToMessageID = update.CallbackQuery.Message.MessageID
c.Bot.Send(msg)
}
go func() {
if err := c.processCallback(update.CallbackQuery); err != nil {
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, err.Error())
msg.ReplyToMessageID = update.CallbackQuery.Message.MessageID
go c.Bot.Send(msg)
}
}()
} else {
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
Expand All @@ -40,15 +42,15 @@ func (c *Client) Run() {
update.Message.Command() == "start" &&
!c.CheckAuth(update.Message.From.UserName) {
msg.Text = "Enter your password"
c.Bot.Send(msg)
go c.Bot.Send(msg)
continue
}

if c.CheckAuth(update.Message.From.UserName) {
go func() {
if err := c.processMessage(update.Message); err != nil {
msg.Text = err.Error()
c.Bot.Send(msg)
go c.Bot.Send(msg)
}
}()
} else {
Expand Down

0 comments on commit 94b9024

Please sign in to comment.