Compare commits
5 Commits
fd19d0fd25
...
017360ba92
Author | SHA1 | Date |
---|---|---|
Jimmy | 017360ba92 | |
Jimmy | 2bead48384 | |
Jimmy | 7c4f46009e | |
Jimmy | 361cb37fe3 | |
Jimmy | cb3a7f9036 |
|
@ -1,6 +1,6 @@
|
|||
# Telegram Python Example
|
||||
|
||||
This is desinged for MicroPython but will work on a desktop
|
||||
This is designed for MicroPython but will work on a desktop
|
||||
|
||||
It'll echo what you send to it
|
||||
|
||||
|
@ -33,3 +33,7 @@ Put your id in config.json
|
|||
## Run
|
||||
|
||||
```pipenv run prod```
|
||||
|
||||
## MicroPython
|
||||
TODO
|
||||
see ```up.py``` and ```boot.py```
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
# This file is executed on every boot (including wake-boot from deepsleep)
|
||||
import esp
|
||||
esp.osdebug(None)
|
||||
|
||||
import network
|
||||
import json
|
||||
|
||||
with open("config.json", 'r') as f:
|
||||
config = json.load(f)
|
||||
|
||||
print(config)
|
||||
|
||||
sta_if = network.WLAN(network.STA_IF)
|
||||
if not sta_if.isconnected():
|
||||
print('connecting to network...')
|
||||
sta_if.active(True)
|
||||
sta_if.connect(config["wifi"]["ssid"], config["wifi"]["password"])
|
||||
|
||||
while not sta_if.isconnected():
|
||||
pass
|
||||
|
||||
print('network config:', sta_if.ifconfig())
|
||||
|
|
@ -1,4 +1,8 @@
|
|||
{
|
||||
"wifi": {
|
||||
"ssid": "Infected",
|
||||
"password": "(m0unt@1n5)"
|
||||
},
|
||||
"token": "",
|
||||
"recipient": ""
|
||||
}
|
8
main.py
8
main.py
|
@ -10,9 +10,9 @@ with open("config.json", 'r') as f:
|
|||
offset = "0"
|
||||
while True:
|
||||
#long poll for response
|
||||
querry = f'limit=1&offset={offset}&timeout=10'
|
||||
querry = 'limit=1&offset={}&timeout=10'.format(offset)
|
||||
try:
|
||||
resp = requests.get(url=f'https://api.telegram.org/bot{config["token"]}/{"getUpdates"}?{querry}')
|
||||
resp = requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "getUpdates", querry))
|
||||
json = resp.json()
|
||||
resp.close()
|
||||
if json.get("result"):
|
||||
|
@ -21,8 +21,8 @@ while True:
|
|||
text = json["result"][0]["message"]["text"]
|
||||
print(text)
|
||||
#echo message
|
||||
querry = f'chat_id={config["recipient"]}&text={text}'
|
||||
requests.get(url=f'https://api.telegram.org/bot{config["token"]}/{"sendMessage"}?{querry}')
|
||||
querry = 'chat_id={}&text={}'.format(config["recipient"], text)
|
||||
requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "sendMessage", querry))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
resp.close()
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
try:
|
||||
import urequests as requests #For MicroPython
|
||||
except :
|
||||
import requests
|
||||
import json
|
||||
import gc
|
||||
from machine import WDT
|
||||
from machine import Pin
|
||||
|
||||
wdt = WDT(timeout=30000)
|
||||
wdt.feed()
|
||||
gc.enable()
|
||||
|
||||
led = Pin(2, Pin.OUT)
|
||||
led.on()
|
||||
|
||||
with open("config.json", 'r') as f:
|
||||
config = json.load(f)
|
||||
|
||||
offset = "0"
|
||||
while True:
|
||||
gc.collect()
|
||||
wdt.feed()
|
||||
#long poll for response
|
||||
querry = 'limit=1&offset={}&timeout=5'.format(offset)
|
||||
try:
|
||||
resp = requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "getUpdates", querry))
|
||||
json = resp.json()
|
||||
resp.close()
|
||||
if json.get("result"):
|
||||
#get latest message
|
||||
offset = json.get("result")[0].get("update_id") + 1
|
||||
text = json["result"][0]["message"]["text"]
|
||||
print(text)
|
||||
if text=="/on":
|
||||
led.on()
|
||||
if text=="/off":
|
||||
led.off()
|
||||
#echo message
|
||||
querry = 'chat_id={}&text={}'.format(config["recipient"], text)
|
||||
requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "sendMessage", querry))
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
resp.close()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue