-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
94 lines (79 loc) · 3.11 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Guifibages Telegram Bot
#
# Copyright 2015 Associació d'Usuaris Guifibages
# Author: Ignacio Torres Masdeu <i@itorres.net>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import requests
import api
def telegram(action, payload):
print("telegramming: |{}| {}".format(token, action))
url = "https://api.telegram.org/bot{}/{}".format(token, action)
app.logger.debug("url: %s" % url)
return requests.post(url, data=payload)
class Message:
def __init__(self, d):
vars(self).update(d)
if 'edited_message' in d:
self.message = self.edited_message
try:
self.chat = self.message['chat']['id']
except AttributeError:
app.logger.error("No message in payload:\n %s", d)
self.is_group = "title" in self.message['chat']
try:
self.parse()
except Exception as e:
app.logger.error("Cannot parse:\n %s\n %s", self.message, e)
def parse(self):
self.text = self.message['text']
if self.text[0] != "/" or " " not in self.text:
return
self.command, self.args = self.text.split(None, 1)
self.command = self.command[1:].lower()
try:
print("Trying", self.command)
getattr(self, self.command)()
except AttributeError:
sendMessage(self.chat,
"{0}: command not found".format(self.command))
except Exception as e:
print("Exception on", self.command, e)
sendMessage(self.chat,
"Error executing `{0}`".format(self.text))
def whois(self):
result = api.whois(self.args)
print("whois result: {}".format(result))
msg = "whois {0}\n{1}".format(result['ip'], result['text'])
sendMessage(self.chat, msg)
def ping(self):
result = api.ping(self.args)
msg = "ping {0}\n{1}".format(result['ip'], result['text'])
sendMessage(self.chat, msg)
def traceroute(self):
self.mtr()
def mtr(self):
result = api.traceroute(self.args)
msg = "traceroute {0}\n{1}".format(result['ip'], result['text'])
sendMessage(self.chat, msg)
def sendMessage(chat_id, text):
payload = {'chat_id': chat_id, 'text': text, 'parse_mode': 'Markdown'}
result = telegram('sendMessage', payload)
app.logger.debug("Answering:\n %s\n%s" % (payload, result.text))
def sendChatAction(chat_id, action):
payload = {'chat_id': chat_id, 'action': action}
result = telegram('sendChatAction', payload)
app.logger.debug("Answering:\n %s\n%s" % (payload, result))