+ Input chan *tgbotapi.Message
+}
+
+var sessions map[int64]*Session
+var mercHost string
+var sessionsCtx context.Context
+
+func initSessions(host string, ctx context.Context) error {
+ sessions = make(map[int64]*Session)
+ mercHost = host
+ sessionsCtx = ctx
+ return nil
+}
+
+func getSession(chat *tgbotapi.Chat) *Session {
+ session, ok := sessions[chat.ID]
+ if !ok {
+ session = newSession(chat)
+ }
+ return session
+}
+
+func newSession(chat *tgbotapi.Chat) *Session {
+ session := Session{chat, make(chan *tgbotapi.Message)}
+ sessions[chat.ID] = &session
+ startSession(&session)
+ return &session
+}
+
+func startSession(session *Session) {
+ ctx, _ := context.WithCancel(sessionsCtx)
+
+ go func() {
+ telnetInput, telnetOutput := make(chan string), make(chan string)
+ caller := TelnetCaller{
+ Input: telnetInput,
+ Output: telnetOutput,
+ }
+
+ go func() {
+ for {
+ select {
+ case msg := <-session.Input:
+ if msg.Text != "/start" {
+ telnetInput <- msg.Text
+ }
+ case body := <-telnetOutput:
+ newMsg := tgbotapi.NewMessage(session.Chat.ID, body)
+ sendToTelegram(newMsg)
+ case <-ctx.Done():
+ return
+ }
+ }
+ }()
+
+ telnet.DialToAndCall(mercHost, caller)
+ }()
+}
+
+func sendToSession(session *Session, message *tgbotapi.Message) {
+ session.Input <- message