-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
151 lines (111 loc) · 3.73 KB
/
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
151
"""
Interface for the bot, webhook and decorators
"""
# Imports to get enviroment variables
import os
# Import the chosen library
import telebot
# Import handlers for decorators
from modules import handler
# Import for logging (debug or info)
import logging
# Imports for webhook
from aiohttp import web
# VARIABLES
# Telegram bot token (provided by @BotFather on Telegram)
API_TOKEN = os.environ.get("TOKEN", "")
# Address where we are setting the webhook
WEBHOOK_HOST = os.environ.get("HOST", "example.org")
WEBHOOK_PORT = os.environ.get("PORT", 80)
WEBHOOK_LISTEN = os.environ.get("LISTEN", "0.0.0.0")
# External url where we want the callbacks to go (nginx)
WEBHOOK_URL_BASE = "https://{}".format(WEBHOOK_HOST)
WEBHOOK_URL_PATH = "/{}/".format(API_TOKEN)
# INITIALIZE BOT
# Create the bot
bot = telebot.TeleBot(API_TOKEN)
# Initialize the info level in logging
logger = telebot.logger
telebot.logger.setLevel(logging.INFO)
# If executed directly
if __name__ == "__main__":
# Create the aiohttp framework server
app = web.Application()
# Create Handler instance
h = handler.Handler()
# Function to handle webhook calls
async def handle(request):
if request.match_info.get('token') == bot.token:
request_body_dict = await request.json()
update = telebot.types.Update.de_json(request_body_dict)
bot.process_new_updates([update])
return web.Response()
else:
return web.Response(status=403)
# HANDLERS
@bot.message_handler(commands=['getImage', 'getimage'])
def handle_image(message):
h.get_image_handler(message)
return
@bot.message_handler(commands=['getVideo', 'getvideo'])
def handle_video(message):
h.get_video_handler(message)
return
@bot.message_handler(commands=['getSalseo', 'getsalseo'])
def handle_salseo(message):
h.get_salseo_handler(message)
return
@bot.message_handler(commands=['getNevera', 'getnevera'])
def handle_nevera(message):
h.get_nevera_handler(message)
return
@bot.message_handler(commands=['test'])
def test(message):
h.test_handler(message)
return
@bot.message_handler(commands=['getid', 'getId'])
def get_chat_id(message):
h.get_id_handler(message)
return
@bot.message_handler(commands=['cast'])
def cast(message):
h.cast_handler(message)
return
@bot.message_handler(commands=['setalias', 'setAlias'])
def set_alias(message):
h.set_camera_alias_handler(message)
return
@bot.message_handler(commands=['getcurrentpeople', 'getCurrentPeople', 'g'])
def get_current_people(message):
h.get_current_people_number_handler(message)
return
"""
@bot.message_handler(commands=['getturn', 'getTurn'])
def get_turn(message):
h.get_turn_handler(message)
return
@bot.message_handler(commands=['turndone', 'turnDone'])
def turn_done(message):
h.turn_done_handler(message)
return
@bot.message_handler(commands=['turnundo', 'turnUndo'])
def turn_undo(message):
h.turn_undo_handler(message)
return
"""
@bot.message_handler(commands=['camerareset', 'cameraReset'])
def reset_camera(message):
h.reset_camera_handler(message)
return
# WEBHOOK
# Set the route and function to handle them for http callbacks
app.router.add_post('/{token}/', handle)
# Tell Telegram to remove previous webhooks (maybe we left one open)
bot.remove_webhook()
# Set webhook
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH)
# Start aiohttp server
web.run_app(
app,
host=WEBHOOK_LISTEN,
port=WEBHOOK_PORT)