-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwikimon-cli
executable file
·62 lines (51 loc) · 2.46 KB
/
wikimon-cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /usr/bin/env python
"""
This is the server starter.
It has all Yowsup core layers to handle the connection, encryption and media handling.
https://github.com/tgalal/yowsup/wiki/Yowsup-2.0-Architecture
The top layer of the stack is the RouteLayer (from route.py).
"""
import logging, time
from yowsup.layers import YowLayerEvent, YowParallelLayer
from yowsup.layers.auth import AuthError
from yowsup.layers.network import YowNetworkLayer
from yowsup.stacks.yowstack import YowStackBuilder
from wikimon_bot.layers.notifications.notification_layer import NotificationsLayer
from wikimon_bot.router import RouteLayer
import argparse
parser = argparse.ArgumentParser(description="Run wikimon")
parser.add_argument("-p", "--phone", metavar="phone number", required=True, type=str, help="Phone number used for registration")
parser.add_argument("-pw", "--password", metavar="password", required=True, type=str, help="14-20 digit password")
args = parser.parse_args()
wikimonauth = (args.phone, args.password)
class YowsupEchoStack(object):
def __init__(self, credentials):
"Creates the stacks of the Yowsup Server,"
self.credentials = credentials
stack_builder = YowStackBuilder().pushDefaultLayers(True)
# on the top stack, the two layers that controls the bot and respond to messages and notifications
# see both of classes for more documentation
stack_builder.push(YowParallelLayer([RouteLayer, NotificationsLayer]))
self.stack = stack_builder.build()
self.stack.setCredentials(credentials)
def start(self):
"Starts the connection with Whatsapp servers,"
self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
try:
logging.info("#" * 50)
logging.info("\tServer started. Phone number: %s" % self.credentials[0])
logging.info("#" * 50)
self.stack.loop(timeout=0.5, discrete=0.5)
except AuthError as e:
logging.exception("Authentication Error: %s" % e.message)
except Exception as e:
logging.exception("Unexpected Exception: %s" % e.message)
if __name__ == "__main__":
import sys
from wikimon_bot import config
logging.basicConfig(stream=sys.stdout, level=config.logging_level, format=config.log_format)
server = YowsupEchoStack(wikimonauth)
while True:
# In case of disconnect, keeps connecting...
server.start()
logging.info("Restarting..")