1
+ const puppeteer = require ( 'puppeteer' ) ;
2
+ const TelegramBot = require ( 'node-telegram-bot-api' ) ;
3
+ var fs = require ( 'fs' ) ;
4
+
5
+ const logo =
6
+ `\n
7
+ /$$$$$$$ /$$ /$$ /$$ /$$ /$$
8
+ | $$__ $$ | $$ | $$ | $$$ /$$$ | $$
9
+ | $$ \\ $$ /$$$$$$ /$$$$$$$ | $$ /$$| $$ | $$$$ /$$$$ /$$$$$$ /$$$$$$ /$$$$$$
10
+ | $$$$$$$ /$$__ $$| $$__ $$| $$ /$$/| $$ | $$ $$/$$ $$ /$$__ $$ /$$__ $$|_ $$_/
11
+ | $$__ $$| $$ \\ $$| $$ \\ $$| $$$$$$/ |__/ | $$ $$$| $$| $$$$$$$$| $$$$$$$$ | $$
12
+ | $$ \\ $$| $$ | $$| $$ | $$| $$_ $$ | $$\\ $ | $$| $$_____/| $$_____/ | $$ /$$
13
+ | $$$$$$$/| $$$$$$/| $$ | $$| $$ \\ $$ /$$ | $$ \\/ | $$| $$$$$$$| $$$$$$$ | $$$$/
14
+ |_______/ \\______/ |__/ |__/|__/ \\__/|__/ |__/ |__/ \\_______/ \\_______/ \\___/
15
+ bot for attending your Gmeet sessions --by bunny \n`
16
+ console . log ( logo ) ;
17
+ console . log ( 'Send /help to the bot...' ) ;
18
+
19
+ const token = process . env . bot_token ;
20
+ const Cookie = JSON . parse ( process . env . cookie ) ;
21
+
22
+ async function main ( ) {
23
+
24
+ const browser = await puppeteer . launch ( {
25
+ headless : true ,
26
+ ignoreDefaultArgs : [ '--enable-automation' ] ,
27
+ args : [ '--start-maximized' ] ,
28
+ defaultViewport : { width : 1920 , height : 1080 }
29
+ } ) ;
30
+ const context = browser . defaultBrowserContext ( ) ;
31
+ await context . overridePermissions ( 'https://meet.google.com' , [ ] ) ;
32
+ const page = await browser . newPage ( ) ;
33
+ await page . setDefaultNavigationTimeout ( 0 ) ;
34
+ await page . setCookie ( ...Cookie ) ;
35
+
36
+ // Create a bot that uses 'polling' to fetch new updates
37
+ const bot = new TelegramBot ( token , { polling : true } ) ;
38
+
39
+ bot . on ( "polling_error" , console . log ) ;
40
+
41
+ // Matches "/echo [whatever]"
42
+ bot . onText ( / \/ j o i n ( .+ ) / , async ( msg , match ) => {
43
+ const chatId = msg . chat . id ;
44
+ const resp = match [ 1 ] ; // the captured "whatever"
45
+ const classCode = resp . match ( / \w + - \w + - \w + / ) [ 0 ] ;
46
+ console . log ( 'Classroom link : ' , `https://meet.google.com/${ classCode } ` ) ;
47
+ await page . goto ( `https://meet.google.com/${ classCode } ` ) ;
48
+ bot . sendMessage ( chatId , 'Please wait...' , { reply_to_message_id : msg . message_id } )
49
+
50
+ let [ button ] = await page . $x ( "//button[contains(., 'Dismiss')]" ) ;
51
+ if ( button ) {
52
+ await button . click ( ) ;
53
+ }
54
+
55
+ [ button ] = await page . $x ( "//button[contains(., 'Return to home screen')]" ) ;
56
+ if ( button ) {
57
+ bot . sendMessage ( chatId , 'Check your meeting code.\nMake sure that you have used the correct meet url.' , { reply_to_message_id : msg . message_id } )
58
+ }
59
+
60
+ else {
61
+
62
+
63
+ xpath = '//*[@id="yDmH0d"]/c-wiz/div/div/div[9]/div[3]/div/div/div[3]/div/div/div[2]/div/div[2]/div/div[1]/div[1]/span/span'
64
+ await page . waitForXPath ( xpath )
65
+ await page . waitForTimeout ( 1000 )
66
+ elements = await page . $x ( xpath )
67
+ await elements [ 0 ] . click ( )
68
+ await page . keyboard . press ( 'Tab' )
69
+ await page . keyboard . press ( 'Tab' )
70
+ await page . keyboard . press ( 'Tab' )
71
+ await page . keyboard . press ( 'Tab' )
72
+ await page . waitForTimeout ( 5000 )
73
+ await page . screenshot ( { path : 'example.png' } ) ;
74
+ let stream = await fs . createReadStream ( './example.png' ) ;
75
+ bot . sendMessage ( chatId , 'Requested/joined the class!' , { reply_to_message_id : msg . message_id } )
76
+ bot . sendPhoto ( chatId , stream ) ;
77
+ }
78
+ } ) ;
79
+
80
+ bot . on ( 'message' , async ( msg ) => {
81
+ const chatId = msg . chat . id ;
82
+ if ( msg . text == '\/status' ) {
83
+ console . log ( msg . text ) ;
84
+ await page . screenshot ( { path : 'example.png' } ) ;
85
+ let stream = await fs . createReadStream ( './example.png' ) ;
86
+ bot . sendPhoto ( chatId , stream ) ;
87
+ }
88
+ else if ( msg . text == '\/leave' ) {
89
+ console . log ( msg . text ) ;
90
+ let xpath = '//*[@id="ow3"]/div[1]/div/div[9]/div[3]/div[10]/div[2]/div/div[7]/span/button'
91
+ await page . waitForTimeout ( 1000 )
92
+ let elements = await page . $x ( xpath )
93
+ if ( elements . length != 0 ) {
94
+ await elements [ 0 ] . click ( )
95
+ console . log ( 'meet left' ) ;
96
+ await page . waitForTimeout ( 2000 )
97
+ bot . sendMessage ( chatId , 'Left the meet!' , { reply_to_message_id : msg . message_id } )
98
+ await page . screenshot ( { path : 'example.png' } ) ;
99
+ let stream = await fs . createReadStream ( './example.png' ) ;
100
+ bot . sendPhoto ( chatId , stream ) ;
101
+ }
102
+ else {
103
+ bot . sendMessage ( chatId , 'You are not attending any gmeet session currently' , { reply_to_message_id : msg . message_id } )
104
+ }
105
+ }
106
+
107
+ else if ( msg . text == '\/help' ) {
108
+ let help = `List of available commands:
109
+ /join {gmeet_link}
110
+ /status (shows the ongoing meet current status)
111
+ /leave (leaves the current gmeet session)
112
+ /help`
113
+ bot . sendMessage ( chatId , help , { reply_to_message_id : msg . message_id } )
114
+ }
115
+
116
+ else if ( msg . text == '\/start' ) {
117
+ bot . sendMessage ( chatId , 'Yo! send /help' , { reply_to_message_id : msg . message_id } )
118
+ }
119
+ } ) ;
120
+ }
121
+
122
+ main ( )
0 commit comments