Skip to content

Instantly share code, notes, and snippets.

@mquezada
Created November 23, 2016 22:16
Show Gist options
  • Save mquezada/f262f7983d0bd73c4f84b639abebd0ef to your computer and use it in GitHub Desktop.
Save mquezada/f262f7983d0bd73c4f84b639abebd0ef to your computer and use it in GitHub Desktop.
import sys
import time
import telepot
import thread
from random import random, randint
from pprint import pprint
with open('mensajes/trigger.txt') as f:
trigger_words = set(f.readlines())
with open('mensajes/preguntas.txt') as f:
preguntas = f.readlines()
with open('mensajes/respuestas.txt') as f:
replies = f.readlines()
with open('mensajes/followup.txt') as f:
follow = f.readlines()
with open('mensajes/iniciales.txt') as f:
iniciales = f.readlines()
def randmsg(msg):
return msg[randint(0, len(msg) - 1)]
def sleep(secs):
time.sleep(random() * secs)
def rand(prob):
if random() > prob:
return True
return False
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
pprint(msg)
# Do your stuff according to `content_type` ...
if content_type == 'text':
text = msg['text']
msg_id = msg['message_id']
c = trigger_words.intersection(set(text.split()))
if '@berniethebot' in text or c:
r_message = randmsg(iniciales)
bot.sendChatAction(chat_id, "typing")
sleep(len(r_message) / letters_per_sec)
bot.sendMessage(chat_id, r_message)
if rand(0.5):
f_message = randmsg(follow)
bot.sendChatAction(chat_id, "typing")
sleep(len(f_message) / (letters_per_sec * random()))
bot.sendMessage(chat_id, f_message)
while rand(0.75):
f_message = randmsg(follow)
bot.sendChatAction(chat_id, "typing")
sleep(len(f_message) / (letters_per_sec * random()))
bot.sendMessage(chat_id, f_message)
elif rand(0.5) and text.endswith("?"):
bot.sendMessage(chat_id,
randmsg(replies),
reply_to_message_id=msg_id)
elif 'reply_to_message' in msg:
reply = msg['reply_to_message']
if reply['from']['username'] == 'berniethebot':
bot.sendMessage(chat_id,
randmsg(replies + preguntas),
reply_to_message_id=msg_id)
while rand(0.75):
f_message = randmsg(follow)
bot.sendChatAction(chat_id, "typing")
sleep(len(f_message) / (letters_per_sec * random()))
bot.sendMessage(chat_id, f_message)
TOKEN = sys.argv[1] # get token from command-line
bot = telepot.Bot(TOKEN)
bot.message_loop(handle)
print ('Listening ...')
# Keep the program running.
while 1:
time.sleep(10)
@JPaulsen
Copy link

Hermoso

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment