Compare commits
9 Commits
0a6e12b04f
...
master
Author | SHA1 | Date | |
---|---|---|---|
ed9b9608d0 | |||
017360ba92 | |||
2bead48384 | |||
7c4f46009e | |||
361cb37fe3 | |||
cb3a7f9036 | |||
fd19d0fd25 | |||
e99d4dc9a3 | |||
bc71e1765b |
27
README.md
27
README.md
@@ -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```
|
```pipenv sync```
|
||||||
|
|
||||||
```cp config.json.sample config.json```
|
```cp config.json.sample config.json```
|
||||||
|
|
||||||
|
|
||||||
Create a Telegram account if you don't have one
|
Create a Telegram account if you don't have one
|
||||||
|
|
||||||
https://telegram.org/
|
https://telegram.org/
|
||||||
|
|
||||||
|
## Setup Bot
|
||||||
Search users for ```BotFather```
|
Search users for ```BotFather```
|
||||||
Click start
|
|
||||||
|
|
||||||
Type ```/newbot``` and follow the prompts
|
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```
|
Search users for ```userinfobot @userinfobot```
|
||||||
Put you id in config.json
|
|
||||||
|
|
||||||
Run
|
Put your id in config.json
|
||||||
|
|
||||||
|
|
||||||
|
## Run
|
||||||
|
|
||||||
```pipenv run prod```
|
```pipenv run prod```
|
||||||
|
|
||||||
|
## MicroPython
|
||||||
|
TODO
|
||||||
|
see ```up.py``` and ```boot.py```
|
||||||
|
23
boot.py
Normal file
23
boot.py
Normal 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())
|
||||||
|
|
@@ -1,4 +1,8 @@
|
|||||||
{
|
{
|
||||||
|
"wifi": {
|
||||||
|
"ssid": ""
|
||||||
|
"password": ""
|
||||||
|
},
|
||||||
"token": "",
|
"token": "",
|
||||||
"recipient": ""
|
"recipient": ""
|
||||||
}
|
}
|
12
main.py
12
main.py
@@ -1,5 +1,5 @@
|
|||||||
try:
|
try:
|
||||||
import urequests as requests
|
import urequests as requests #For MicroPython
|
||||||
except :
|
except :
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
@@ -9,10 +9,10 @@ with open("config.json", 'r') as f:
|
|||||||
|
|
||||||
offset = "0"
|
offset = "0"
|
||||||
while True:
|
while True:
|
||||||
querry = "limit=1&offset={}&timeout=10".format(offset)
|
|
||||||
#long poll for response
|
#long poll for response
|
||||||
|
querry = 'limit=1&offset={}&timeout=10'.format(offset)
|
||||||
try:
|
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()
|
json = resp.json()
|
||||||
resp.close()
|
resp.close()
|
||||||
if json.get("result"):
|
if json.get("result"):
|
||||||
@@ -20,9 +20,9 @@ while True:
|
|||||||
offset = json.get("result")[0].get("update_id") + 1
|
offset = json.get("result")[0].get("update_id") + 1
|
||||||
text = json["result"][0]["message"]["text"]
|
text = json["result"][0]["message"]["text"]
|
||||||
print(text)
|
print(text)
|
||||||
#send acknowledgement
|
#echo message
|
||||||
querry = "chat_id={}&text={}".format(config["recipient"], text)
|
querry = 'chat_id={}&text={}'.format(config["recipient"], text)
|
||||||
requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "sendMessage" ,querry))
|
requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "sendMessage", querry))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
resp.close()
|
resp.close()
|
||||||
|
54
up.py
Normal file
54
up.py
Normal 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()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user