Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
bits_tg_bot
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Operations
Operations
Incidents
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Fred
bits_tg_bot
Commits
83997741
Commit
83997741
authored
Oct 09, 2020
by
Fred
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parent
d54e26f3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
0 deletions
+53
-0
main.py
main.py
+53
-0
No files found.
main.py
0 → 100644
View file @
83997741
import
websockets
import
asyncio
from
telegram
import
Bot
import
json
WS_URL
=
"wss://bits.poul.org/ws"
CHAT_ID
=
123
#CHANGE ME
MESSAGE_ID
=
123
#CHANGE ME
TOKEN
=
"KEK"
bot
=
Bot
(
TOKEN
)
def
bits_handler
(
text
):
meme
=
json
.
loads
(
text
)
bot
.
edit_message_text
(
"POuL HQ is now{}"
.
format
(
meme
[
"status"
][
"value"
]),
CHAT_ID
,
MESSAGE_ID
)
return
class
WebSocketClient
:
def
__init__
(
self
,
ws_url
):
self
.
ws_url
=
ws_url
self
.
connection
=
None
async
def
connect
(
self
):
self
.
connection
=
await
websockets
.
client
.
connect
(
self
.
ws_url
)
if
self
.
connection
.
open
:
return
self
.
connection
async
def
receiveMessage
(
self
,
connection
,
message_handler
):
while
True
:
try
:
message
=
await
connection
.
recv
()
message_handler
(
message
)
except
websockets
.
exceptions
.
ConnectionClosed
:
break
async
def
heartbeat
(
self
):
while
True
:
try
:
await
self
.
connection
.
ping
()
await
asyncio
.
sleep
(
5
)
except
websockets
.
exceptions
.
ConnectionClosed
:
break
if
__name__
==
'__main__'
:
client
=
WebSocketClient
(
WS_URL
)
loop
=
asyncio
.
get_event_loop
()
connection
=
loop
.
run_until_complete
(
client
.
connect
())
tasks
=
[
asyncio
.
ensure_future
(
client
.
heartbeat
()),
asyncio
.
ensure_future
(
client
.
receiveMessage
(
connection
,
bits_handler
)),
]
loop
.
run_until_complete
(
asyncio
.
wait
(
tasks
))
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment