/newbot < > <username >
pip install requests
# -*- coding: utf-8 -*- import requests import time import subprocess import os #import mailchecker requests.packages.urllib3.disable_warnings() # InsecureRequestWarning, # Telegram @BotFather # ADMIN_ID - ( ), # ID, () # GET # , <token> : # https://api.telegram.org/bot<token>/getUpdates # , "from":{"id":01234567,"first_name":"Name","username":"username"} # , # , "id" - # ADMIN_ID TOKEN INTERVAL = 3 # () ADMIN_ID = 12345678 # ID . URL = 'https://api.telegram.org/bot' # HTTP Bot API TOKEN = '123456789:???????????????????????????????????' # offset = 0 # ID def check_updates(): """ , """ global offset data = {'offset': offset + 1, 'limit': 5, 'timeout': 0} # try: request = requests.post(URL + TOKEN + '/getUpdates', data=data) # except: log_event('Error getting updates') # return False # if not request.status_code == 200: return False # if not request.json()['ok']: return False # API for update in request.json()['result']: # offset = update['update_id'] # ID # , 'message' # 'message' 'text', if not 'message' in update or not 'text' in update['message']: log_event('Unknown update: %s' % update) # continue # from_id = update['message']['chat']['id'] # ID () name = update['message']['chat']['username'] # username if from_id <> ADMIN_ID: # , send_text("You're not autorized to use me!", from_id) # log_event('Unautorized: %s' % update) # continue # message = update['message']['text'] # parameters = (offset, name, from_id, message) log_event('Message (id%s) from %s (id%s): "%s"' % parameters) # ID # , run_command(*parameters) def run_command(offset, name, from_id, cmd): if cmd == '/ping': # ping send_text(from_id, 'pong') # elif cmd == '/help': # help send_text(from_id, 'No help today. Sorry.') # elif cmd == '/photo': # Web- # If . - , , # , # , if make_photo(offset) or make_photo(offset): # , requests.post(URL + TOKEN + '/sendChatAction', data={'chat_id': from_id, 'action': 'upload_photo'}) send_photo(from_id, offset) # else: send_text(from_id, 'Error occured') # , elif cmd == '/mail': check_mail() # else: send_text(from_id, 'Got it.') # def log_event(text): """ ToDo: 1) """ event = '%s >> %s' % (time.ctime(), text) print event def send_text(chat_id, text): """ chat_id ToDo: """ log_event('Sending to %s: %s' % (chat_id, text)) # data = {'chat_id': chat_id, 'text': text} # request = requests.post(URL + TOKEN + '/sendMessage', data=data) # HTTP if not request.status_code == 200: # return False # return request.json()['ok'] # API def make_photo(photo_id): """ fswebcam Web-""" photo_name = 'photo/%s.jpg' % photo_id # subprocess.call('fswebcam -q -r 1280x720 %s' % photo_name, shell=True) # shell- return os.path.exists(photo_name) # , def send_photo(chat_id, photo_id): """ """ data = {'chat_id': chat_id} # photo_name = 'photo/%s.jpg' % photo_id # if not os.path.exists(photo_name): return False # files = {'photo': open(photo_name, 'rb')} # request = requests.post(URL + TOKEN + '/sendPhoto', data=data, files=files) # return request.json()['ok'] # True False, , def check_mail(): """ """ print " " return False try: log_event('Checking mail...') # respond = mailchecker.check_all() # except: log_event('Mail check failed.') # return False # if not respond: respond = 'No new mail.' # , send_text(ADMIN_ID, respond) # return True if __name__ == "__main__": while True: try: check_updates() time.sleep(INTERVAL) except KeyboardInterrupt: print ' ..' break
python telegram.py CTRL+Z bg
python < >/telegram.py
nano /etc/rc.local ... python /home/pi/telegram.py exit 0
Source: https://habr.com/ru/post/261473/
All Articles