2 midgaard_bot, a Telegram bot which sets a bridge to Midgaard Merc MUD
3 Copyright (C) 2017 by Javier Sancho Fernandez <jsf at jsancho dot org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 "github.com/go-telegram-bot-api/telegram-bot-api"
30 Input chan *tgbotapi.Message
33 var sessions map[int64]*Session
34 var sessionsCtx context.Context
36 func initSessions(ctx context.Context) error {
37 sessions = make(map[int64]*Session)
42 func getSession(chat *tgbotapi.Chat) *Session {
43 session, ok := sessions[chat.ID]
45 session = newSession(chat)
50 func newSession(chat *tgbotapi.Chat) *Session {
51 session := Session{chat, make(chan *tgbotapi.Message)}
52 sessions[chat.ID] = &session
53 startSession(&session)
57 func startSession(session *Session) {
58 ctx, _ := context.WithCancel(sessionsCtx)
62 case msg := <-session.Input:
63 log.Printf("[%s] %s", msg.From.UserName, msg.Text)
65 newMsg := tgbotapi.NewMessage(msg.Chat.ID, msg.Text)
66 newMsg.ReplyToMessageID = msg.MessageID
67 sendToTelegram(newMsg)
75 func sendToSession(session *Session, message *tgbotapi.Message) {
76 session.Input <- message