-
Notifications
You must be signed in to change notification settings - Fork 4
/
telegram_bot.py
executable file
·150 lines (118 loc) · 4.22 KB
/
telegram_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
# Psiphon Telegram Bots
# Copyright (C) 2021 Psiphon Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
import importlib
import json
import os
from pathlib import Path
from telegram import (
InlineKeyboardButton,
InlineKeyboardMarkup,
)
from telegram.ext import (
CallbackQueryHandler,
CommandHandler,
Updater,
)
from logger import get_logger
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--language", help="language code as in ./lang/. Default: en")
parser.add_argument(
"-t",
"--token",
help="path to JSON file containing Telegram bot HTTP API token",
metavar="PATH",
)
args = parser.parse_args()
if args.language:
language = args.language
else:
language = os.getenv("TELEGRAM_BOT_LANGUAGE")
if not language:
raise Exception("no language found")
log = get_logger(
f"telegram_bot:{language}",
log_path=Path(os.path.join(os.path.dirname(__file__), f"telegram_bot_{language}.log")),
)
def checkTokenEnv(language, logger):
token_env_path = os.getenv("TELEGRAM_BOT_TOKEN_PATH")
if token_env_path:
try:
token = json.loads(Path(token_env_path).read_text())[language]
return token
except FileNotFoundError:
log.info(f"No file found at {token_env_path}, will check for token")
token = os.getenv("TELEGRAM_BOT_TOKEN")
if not token:
raise Exception("No token or token file found")
return token
if args.token:
token_path = args.token
try:
token = json.loads(Path(token_path).read_text())[language]
except FileNotFoundError:
log.info(f"No file found at {token_path}, will check env vars for token")
token = checkTokenEnv(language, log)
else:
token = checkTokenEnv(language, log)
# import
s = importlib.import_module(f"lang.{language}.states")
states = s.states
# token is added to bot
updater = Updater(token)
dispatcher = updater.dispatcher
##### Bot Entrypoint
def getStartHandler(text, reply_markup):
def handler(bot, update):
log.info("new interaction started")
update.message.reply_text(text, reply_markup=reply_markup)
return CommandHandler("start", handler)
def getCallBackQueryHandler(text, pattern, reply_markup=None, log_info=None):
def handler(bot, update):
if log_info:
log.info(log_info)
query = update.callback_query
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=text,
reply_markup=reply_markup if reply_markup else None,
)
return CallbackQueryHandler(handler, pattern=pattern)
# more action items come here / callback_data option add them here and remove "pass"
# then add the message in the messages
def first_submenu(bot, update):
pass
##### Keyboards
def getInlineKeyboard(buttons):
keyboard = []
for row in buttons:
current_row = []
for button in row:
current_row.append(InlineKeyboardButton(button["text"], callback_data=button["next_item"]))
keyboard.append(current_row)
return InlineKeyboardMarkup(keyboard)
##### Handlers
for state in states.values():
keyboard = None
if "keyboard" in state:
keyboard = getInlineKeyboard(state["keyboard"])
if state["pattern"] == "activity":
updater.dispatcher.add_handler(getStartHandler(state["message"], keyboard))
updater.dispatcher.add_handler(getCallBackQueryHandler(state["message"], state["pattern"], reply_markup=keyboard))
print(updater.bot.username, "is listening...")
updater.start_polling()