Compare commits

...

5 Commits

Author SHA1 Message Date
Jimmy 0a6e12b04f Update readme 2021-07-06 22:30:51 +12:00
Jimmy 98d66528b9 Add pipfile 2021-07-06 22:30:21 +12:00
Jimmy 83f0cbc7fa Sample config 2021-07-06 22:30:04 +12:00
Jimmy 1cca5c32d7 Ignore config.json 2021-07-06 22:29:54 +12:00
Jimmy e779e3516f Ignore config.json 2021-07-06 22:29:15 +12:00
6 changed files with 136 additions and 0 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,22 @@
# telegramapi
```pipenv sync```
```cp config.json.sample config.json```
Create a Telegram account if you don't have one
https://telegram.org/
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
Search users for ```userinfobot @userinfobot```
Put you id in config.json
Run
```pipenv run prod```

4
config.json.sample Normal file
View File

@ -0,0 +1,4 @@
{
"token": "",
"recipient": ""
}

34
main.py Normal file
View File

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