Compare commits

...

9 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
5 changed files with 109 additions and 11 deletions

View File

@@ -1,22 +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```
Click start
Type ```/newbot``` and follow the prompts
Take the token under ```Use this token to access the HTTP API:``` and put it into config.json
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 you id in config.json
Run
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": ""
"password": ""
},
"token": "",
"recipient": ""
}

12
main.py
View File

@@ -1,5 +1,5 @@
try:
import urequests as requests
import urequests as requests #For MicroPython
except :
import requests
import json
@@ -9,10 +9,10 @@ with open("config.json", 'r') as f:
offset = "0"
while True:
querry = "limit=1&offset={}&timeout=10".format(offset)
#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))
resp = requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "getUpdates", querry))
json = resp.json()
resp.close()
if json.get("result"):
@@ -20,9 +20,9 @@ while True:
offset = json.get("result")[0].get("update_id") + 1
text = json["result"][0]["message"]["text"]
print(text)
#send acknowledgement
querry = "chat_id={}&text={}".format(config["recipient"], text)
requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "sendMessage" ,querry))
#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()