-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideocallbot.py
124 lines (105 loc) · 3.94 KB
/
videocallbot.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
#
# 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 <http://www.gnu.org/licenses/>.
#
# ---
# Copyright (C) 2015 - Filipe de O. Saraiva <[email protected]>
#
from datetime import datetime as time
from twx.botapi import TelegramBot, Update, Chat, ReplyKeyboardMarkup, User
import json
import os.path
import requests
# Main function to creation of the room, the message, and send
# the reply to the user.
def handle(update):
chat = extractChat(update)
if update.message.text == '/start':
keyboard = createKeyboard()
bot.send_message(chat, 'Press the button if you need me',
reply_markup=keyboard).wait()
if update.message.text == 'Create a video call room':
global numRooms
message = createMessage(update)
bot.send_message(chat, message).wait()
numRooms = numRooms + 1
writeLog(numRooms)
# Extract chat identifier
def extractChat(update):
return update.message.chat.id
# Create custom application keyboard
def createKeyboard():
keyboard = [['Create a video call room']]
return ReplyKeyboardMarkup.create(keyboard)
# Create the message to be sent. This function will be accountable for
# create all elements of a message.
def createMessage(update):
senderName = extractSenderName(update)
roomAddress = createRoom()
return 'Hi, ' + senderName + ' wants to make a video call with you!\n'\
+ 'Click at the address ' + roomAddress + '\n\n'\
+ '@VideoCallBot, bringing video calls to Telegram!'
# Extract sender name from the update message
def extractSenderName(update):
senderFirstName = update.message.sender.first_name
senderLastName = update.message.sender.last_name
senderName = str(senderFirstName) + ' ' + str(senderLastName)
return senderName
# Function to create random room in the conferences website.
# For the moment it is using the appear.in API to create
# funny names for the rooms.
def createRoom():
roomNameJson = requests.get('https://api.appear.in/random-room-name')
roomName = roomNameJson.json()['roomName']
roomAddress = conferencesSite + roomName
return roomAddress
# How many rooms were created? This function will write this
# information in a file for each 500 rooms created.
def writeLog(rooms):
if rooms % 500 == 0:
file = open('numRooms.log', 'a')
file.write(time.now().strftime('%Y-%m-%d') + ' ' + str(rooms) + '\n')
file.close()
# Read numRooms.log and initialize numRooms with the previous number
# of rooms created. If there is not numRooms.log file, initialize the
# number of rooms with 0.
def readNumRooms():
if os.path.exists('numRooms.log'):
file = open('numRooms.log', 'r')
line = file.readlines()[-1]
rooms = int(line.split(' ')[-1])
file.close()
else:
rooms = 0
return rooms
# Load config.py
exec(open('./config.py').read())
# Bot configuration
bot = TelegramBot(botToken)
bot.update_bot_info().wait()
# Website address for creation of video calls
conferencesSite = 'https://appear.in'
# Count the number of rooms created
numRooms = readNumRooms()
# Updates received
offset = 0
# The bot will get updates
while True:
updates = bot.get_updates(offset=offset).wait()
if updates != []:
try:
offset = updates[-1].update_id + 1
except (IndexError, AttributeError):
pass
for update in updates:
handle(update)