7 "github.com/go-telegram-bot-api/telegram-bot-api"
12 Input chan *tgbotapi.Message
15 var sessions map[int64]*Session
16 var sessionsCtx context.Context
18 func initSessions(ctx context.Context) error {
19 sessions = make(map[int64]*Session)
24 func getSession(chat *tgbotapi.Chat) *Session {
25 session, ok := sessions[chat.ID]
27 session = newSession(chat)
32 func newSession(chat *tgbotapi.Chat) *Session {
33 session := Session{chat, make(chan *tgbotapi.Message)}
34 sessions[chat.ID] = &session
35 startSession(&session)
39 func startSession(session *Session) {
40 ctx, _ := context.WithCancel(sessionsCtx)
44 case msg := <-session.Input:
45 log.Printf("[%s] %s", msg.From.UserName, msg.Text)
47 newMsg := tgbotapi.NewMessage(msg.Chat.ID, msg.Text)
48 newMsg.ReplyToMessageID = msg.MessageID
49 sendToTelegram(newMsg)
57 func sendToSession(session *Session, message *tgbotapi.Message) {
58 session.Input <- message