Skip to content

Instantly share code, notes, and snippets.

@bembu
Last active October 17, 2016 15:20
Show Gist options
  • Save bembu/688c342ac5b3798e463f43ffa5570b91 to your computer and use it in GitHub Desktop.
Save bembu/688c342ac5b3798e463f43ffa5570b91 to your computer and use it in GitHub Desktop.
MB #3 -palvelinskripti
"""
Palvelinskripti.
Laheta pyynto osoitteeseen localhost:5000/photo
(esim. avaamalla selaimen). Kamera ottaa kuvan
ja lahettaa sen Telegram-kanavalle.
Koodista on vaihdettava <api-token> ja <group-id>,
jotta se toimii.
>> python selfie_cam.py
@author Peter Kronstrom
"""
from flask import Flask
import requests
import picamera
app = Flask(__name__)
@app.route('/photo', methods=['GET','POST'])
def handle_photo():
camera_capture()
send_to_telegram()
return '{"success":"true"}'
def camera_capture():
with picamera.PiCamera() as camera:
with open('image.jpg', 'wb') as capture_file:
camera.capture(capture_file)
def send_to_telegram():
with open('image.jpg', 'rb') as capture_file:
url = 'https://api.telegram.org/bot<api-token>/sendPhoto'
f = {"photo": capture_file}
p = {'chat_id': <group-id>}
res = requests.post(url, files=f, params=p)
app.run(host="0.0.0.0")
@bembu
Copy link
Author

bembu commented Oct 10, 2016

Testattu toimivaksi python3:lla

@bembu
Copy link
Author

bembu commented Oct 10, 2016

Testattu toimivaksi python2.7:lla

@bembu
Copy link
Author

bembu commented Oct 10, 2016

Tested to be working on Python 3 and Python 2.7.

Installation:
sudo pip install Flask requests picamera

Run:
python selfie_cam.py

@bembu
Copy link
Author

bembu commented Oct 11, 2016

Replace api-token (String) and group-id (Int) before running

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment