在tg对话框查询数据库,并获取结果文件

330 人阅读
分类:

               

import time
import os
import telepot
from telepot.loop import MessageLoop
from subprocess import Popen, PIPE

def handle(msg):
    print("==================================")
    content_type, chat_type, chat_id = telepot.glance(msg)
    print(content_type, chat_type, chat_id)

#    if msg['text'] == '/food':
#        bot.sendMessage(chat_id, 'hamburger')
#    elif msg['text'] == '/drink':
#        bot.sendMessage(chat_id, 'coke zero')
    os.system("rm -rf /data/result")
    sql=msg['text'].split('//')[1]
    cmd='mysql -utg -h 1.1.1.1 -pabce1234 -e' + '"' + "use testdb;" + sql + '"' + ">result"
    os.system(cmd)
    print("111111111111111111")
    #message=os.popen(cmd)
    #pro = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
    #out, err = pro.communicate()
    time.sleep(5)
    if os.stat("/data/result").st_size == 0:
        print("22222222222")
        ncmd="curl -X POST "+'"https://api.telegram.org/bot1111111111:AAEwaaESCqSswQOEU3CA7LUEzypK9tpE0YVI/sendMessage" -d "chat_id=-111111111&text='+bytes.decode(err)+'"'
        os.system(ncmd)
        time.sleep(3)
    else:
        print("33333333333")
        cmd1='curl -v -F  "chat_id=-111111111" -F document=@/data/result https://api.telegram.org/bot1111111111:AAEwaaESCqSswQOEU3CA7LUEzypK9tpE0YVI/sendDocument'
        message1=os.popen(cmd1)
        time.sleep(5)
#        os.system("rm -rf /data/result")
TOKEN = '1111111111:AAEwaaESCqSswQOEU3CA7LUEzypK9tpE0YVI'
bot = telepot.Bot(TOKEN)
MessageLoop(bot, handle).run_as_thread()
print ('Listening ...')

# Keep the program running.
while 1:
    time.sleep(10)


和博主交个朋友吧
    发布篇幅
    • 文章总数:0
    • 原创:0
    • 转载:0
    • 译文:0
    文章分类
      文章存档
      阅读排行