티스토리 뷰

[Python] args를 받아 Telegram BOT으로 전송


 

아래의 libraryBot.py 는 간단하다. 다른 스크립트에서 args 를 넘겨받아 Telegram BOT으로 전송하는 스크립트이다.  

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
#!/usr/bin/python
 
import telegram
import subprocess
import sys
 
my_token = 'BOT TOKEN 입력'
 
bot = telegram.Bot(token = my_token)
 
updates = bot.getUpdates()
 
for u in updates :
        print(u.message)
 
chat_id = 'CHAT ID 입력'
 
args=sys.argv[1:]
cmd = ' '
 
for arg in args:
        cmd += " " + arg
 
#for i in range(1, len(sys.argv)):
#       print(sys.argv[i])
 
#print result
result = subprocess.check_output(cmd, shell=True)
bot.sendMessage(chat_id=chat_id, text=result)



실제 다른 스크립트에서 libraryBot.py 호출하여 args를 전달하는 과정은 간단하다.


- 쉘 스크립트에서 파일내용을 Telegram BOT에 전송

echo "$(/usr/bin/python /home/gukii/TelegramAlert/libraryBot.py 'cat /home/gukii/전송하고 싶은 파일명')"


- Perl에서 파일내용을 Telegram BOT에 전송

system("/usr/bin/python /home/gukii/TelegramAlert/libraryBot.py \"cat /home/gukii/전송하고 싶은 파일명\"");


댓글