-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbot.py
executable file
·138 lines (117 loc) · 5.59 KB
/
bot.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/python
import time
import base64
from irclib import IRC, ServerConnectionError
from Yowsup.connectionmanager import YowsupConnectionManager
from config import *
# init yowsup
ycm = YowsupConnectionManager()
signalsInterface = ycm.getSignalsInterface()
methodsInterface = ycm.getMethodsInterface()
# temp var for the creation of group
creator = ""
# first connect to irc
client = IRC()
try:
irc = client.server().connect(irc_server, irc_port, irc_nickname)
except ServerConnectionError as x:
print x
exit(1)
def getNick(jid):
try:
phone_number = jid.split('@')[0]
nick = wa_contacts[phone_number]
except KeyError:
nick = "unknown"
return nick
def ircOnPubMsg(connection, event):
text = event.source().split('!')[0] + "> " + event.arguments()[0]
if wa_group == "":
print "set a whatsapp group first!"
else:
try:
text = text.encode("utf-8")
except UnicodeDecodeError:
pass
methodsInterface.call("message_send", (wa_group, text))
def ircOnPrivMsg(connection, event):
print event.arguments
def ircOnConnect(connection, event):
irc.join(irc_channel)
def ircOnDisconnect(connection, event):
print "irc disconnected"
exit(0)
def waOnGroupCreate(groupJid):
global wa_group
wa_group = groupJid
print irc_channel + " group created! !!!!!! JID: " + groupJid + " !!!!!!"
text = irc_channel + " was created, please type 'add' to join"
methodsInterface.call("message_send", (creator, text.encode("utf-8")))
def waGroupCreateFail(errorCode):
print irc_channel + " creation failed: " + errorCode
def waOnAuthFailed(username, reason):
print "Logging failed because %s" % reason
def waOnAuthSuccess(username):
print "Logged in with %s" % username
methodsInterface.call("ready")
def waOnMessageReceived(messageId, jid, messageContent, timestamp, wantsReceipt, pushName, isBroadCast):
if messageContent.lower() == "add":
text = "you have been added to " + irc_channel
methodsInterface.call("group_addParticipant", (wa_group, jid))
methodsInterface.call("message_send", (jid, text.encode("utf-8")))
if messageContent.lower() == "help":
text = "say 'create' to let me create the groupschat if it is not existing yet or say 'add' and I will let you join " + irc_channel
methodsInterface.call("message_send", (jid, text.encode("utf-8")))
if messageContent.lower() == "create":
global creator
creator = jid
if wa_group == "":
methodsInterface.call("group_create", (irc_channel,))
else:
text = "there is already a channel, ask me to 'add' you - if not please change the config.py and ask again"
methodsInterface.call("message_send", (jid, text.encode("utf-8")))
methodsInterface.call("message_ack", (jid, messageId))
def waOnGroupMessageReceived(messageId, jid, author, messageContent, timestamp, wantsReceipt, pushName):
global wa_group
if wa_group == "":
print "setting group to " + jid
wa_group = jid
irc.privmsg(irc_channel, "[" + getNick(author) + "] " + messageContent)
methodsInterface.call("message_ack", (jid, messageId))
def waOnGroupImageReceived(msgId, fromAttribute, author, mediaPreview, mediaUrl, mediaSize, wantsReceipt):
irc.privmsg(irc_channel, "[" + getNick(author) + " image@wa] " + mediaUrl)
#methodsInterface.call("notification_ack", (author, msgId))
methodsInterface.call("message_ack", (fromAttribute, msgId))
def waNotificationGroupParticipantAdded(groupJid, jid, author, timestamp, messageId, receiptRequested):
irc.privmsg(irc_channel, "[" + getNick(jid) + " joined " + irc_channel + "@wa]")
methodsInterface.call("notification_ack", (groupJid, messageId))
def waNotificationGroupParticipantRemoved(groupJid, jid, author, timestamp, messageId, receiptRequested):
irc.privmsg(irc_channel, "[" + getNick(jid) + " left " + irc_channel + "@wa]")
methodsInterface.call("notification_ack", (groupJid, messageId))
def onGroupLocationReceived(msgId, fromAttribute, author, name, mediaPreview, mlatitude, mlongitude, wantsReceipt):
url = "http://maps.google.com/?q=" + mlatitude + "," + mlongitude
irc.privmsg(irc_channel, "[" + getNick(author) + " location@wa] " + url)
methodsInterface.call("message_ack", (fromAttribute, msgId))
wa_password = base64.b64decode(bytes(wa_password.encode('utf-8')))
# whatsapp handlers
signalsInterface.registerListener("auth_fail", waOnAuthFailed)
signalsInterface.registerListener("auth_success", waOnAuthSuccess)
signalsInterface.registerListener("message_received", waOnMessageReceived)
signalsInterface.registerListener("group_messageReceived", waOnGroupMessageReceived)
signalsInterface.registerListener("group_createSuccess", waOnGroupCreate)
signalsInterface.registerListener("group_createFail", waGroupCreateFail)
signalsInterface.registerListener("group_imageReceived", waOnGroupImageReceived)
signalsInterface.registerListener("notification_groupParticipantAdded", waNotificationGroupParticipantAdded)
signalsInterface.registerListener("notification_groupParticipantRemoved", waNotificationGroupParticipantRemoved)
signalsInterface.registerListener("group_locationReceived", onGroupLocationReceived)
# irc handlers
irc.add_global_handler("disconnect", ircOnDisconnect)
irc.add_global_handler("welcome", ircOnConnect)
irc.add_global_handler("privmsg", ircOnPrivMsg)
irc.add_global_handler("pubmsg", ircOnPubMsg)
# connect to whatsapp
methodsInterface.call("auth_login", (wa_username, wa_password))
# irc 'forever' main loop
# keeps all threads alive
# ctrl-c to break
client.process_forever()