Compare commits

...

14 Commits

Author SHA1 Message Date
ed9b9608d0 Remove details 2021-07-09 11:37:19 +00:00
017360ba92 Update readme 2021-07-08 23:11:13 +12:00
2bead48384 Add wifi 2021-07-08 23:08:44 +12:00
7c4f46009e MicroPython doesn't like f strings 2021-07-08 23:05:44 +12:00
361cb37fe3 Add boot file 2021-07-08 23:04:49 +12:00
cb3a7f9036 Add MicroPython specific version 2021-07-08 23:03:36 +12:00
fd19d0fd25 Install pipenv 2021-07-06 22:53:12 +12:00
e99d4dc9a3 Update readme 2021-07-06 22:40:36 +12:00
bc71e1765b Comments Use f string 2021-07-06 22:36:03 +12:00
0a6e12b04f Update readme 2021-07-06 22:30:51 +12:00
98d66528b9 Add pipfile 2021-07-06 22:30:21 +12:00
83f0cbc7fa Sample config 2021-07-06 22:30:04 +12:00
1cca5c32d7 Ignore config.json 2021-07-06 22:29:54 +12:00
e779e3516f Ignore config.json 2021-07-06 22:29:15 +12:00
8 changed files with 235 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
config.json

17
Pipfile Normal file
View File

@@ -0,0 +1,17 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
requests = "*"
[requires]
python_version = "3.8"
[scripts]
dev = 'bash -c "export DEBUG=1 && ls main.py | entr -rc python main.py"'
prod = "python main.py"

60
Pipfile.lock generated Normal file
View File

@@ -0,0 +1,60 @@
{
"_meta": {
"hash": {
"sha256": "acbc8c4e7f2f98f1059b2a93d581ef43f4aa0c9741e64e6253adff8e35fbd99e"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.8"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"certifi": {
"hashes": [
"sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c",
"sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830"
],
"version": "==2020.12.5"
},
"chardet": {
"hashes": [
"sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa",
"sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'",
"version": "==4.0.0"
},
"idna": {
"hashes": [
"sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6",
"sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
"version": "==2.10"
},
"requests": {
"hashes": [
"sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804",
"sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e"
],
"index": "pypi",
"version": "==2.25.1"
},
"urllib3": {
"hashes": [
"sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c",
"sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'",
"version": "==1.26.5"
}
},
"develop": {}
}

View File

@@ -1,2 +1,39 @@
# telegramapi
# Telegram Python Example
This is designed for MicroPython but will work on a desktop
It'll echo what you send to it
## Setup
```pip3 install pipenv```
```pipenv sync```
```cp config.json.sample config.json```
Create a Telegram account if you don't have one
https://telegram.org/
## Setup Bot
Search users for ```BotFather```
Type ```/newbot``` and follow the prompts
Take the token under ```Use this token to access the HTTP API:``` and put it into config.json
## Get User Id
Search users for ```userinfobot @userinfobot```
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())

8
config.json.sample Normal file
View File

@@ -0,0 +1,8 @@
{
"wifi": {
"ssid": ""
"password": ""
},
"token": "",
"recipient": ""
}

34
main.py Normal file
View File

@@ -0,0 +1,34 @@
try:
import urequests as requests #For MicroPython
except :
import requests
import json
with open("config.json", 'r') as f:
config = json.load(f)
offset = "0"
while True:
#long poll for response
querry = 'limit=1&offset={}&timeout=10'.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)
#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()

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