Compare commits

...

5 Commits

Author SHA1 Message Date
Jimmy 017360ba92 Update readme 2021-07-08 23:11:13 +12:00
Jimmy 2bead48384 Add wifi 2021-07-08 23:08:44 +12:00
Jimmy 7c4f46009e MicroPython doesn't like f strings 2021-07-08 23:05:44 +12:00
Jimmy 361cb37fe3 Add boot file 2021-07-08 23:04:49 +12:00
Jimmy cb3a7f9036 Add MicroPython specific version 2021-07-08 23:03:36 +12:00
5 changed files with 90 additions and 5 deletions

View File

@ -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```

23
boot.py Normal file
View File

@ -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())

View File

@ -1,4 +1,8 @@
{
"wifi": {
"ssid": "Infected",
"password": "(m0unt@1n5)"
},
"token": "",
"recipient": ""
}

View File

@ -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()

54
up.py Normal file
View File

@ -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()