Skip to content
Permalink
Browse files
added logging, added case for user's first message
  • Loading branch information
Fred Cook committed Nov 5, 2018
1 parent bb02e7c commit a32ac2cbe5c06686f10b78cfd7cc85198776981b
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 54 deletions.
@@ -0,0 +1 @@
logs.txt
@@ -2,10 +2,12 @@

This is a chatbot application written in python and integrated into telegram

it is capable of general conversation, providing game(s), querying the google API for information that the user may ask it for
it is capable of general conversation, providing game(s), querying the google API for information that the user may ask it for

Made by :
Fred Cook - cookf2@uni.coventry.ac.uk #need to add others details as well if they want it on the readme


Acknowledgements:
https://www.codementor.io/garethdwyer/building-a-telegram-bot-using-python-part-1-goi5fncay - the "get_url" function in my code is taken
from this tutorial as well as other pointers

@@ -0,0 +1,131 @@
INFO : 2018-11-04 18:56:19,622 : New program run

INFO : 2018-11-04 18:56:24,793 : successful start loop run
INFO : 2018-11-04 18:56:29,903 : successful start loop run
ERROR : 2018-11-04 18:56:30,013 :
Traceback (most recent call last):
File "C:\Users\fred laptop\Google Drive\Uni Work\Activity Led Learning Projects\Python_Telegram_Chat_bot\chat_bot_main.py", line 104, in chat_bot
logging.info('message_id = {}\ntext = {}\nreply = {]'.format(message_id,text,send))
ValueError: expected '}' before end of string
INFO : 2018-11-04 18:56:30,207 : message popped
ERROR : 2018-11-04 18:56:30,207 :
Traceback (most recent call last):
File "C:\Users\fred laptop\Google Drive\Uni Work\Activity Led Learning Projects\Python_Telegram_Chat_bot\chat_bot_main.py", line 104, in chat_bot
logging.info('message_id = {}\ntext = {}\nreply = {]'.format(message_id,text,send))
ValueError: expected '}' before end of string
INFO : 2018-11-04 18:56:30,464 : message popped
INFO : 2018-11-04 18:56:35,481 : successful start loop run
INFO : 2018-11-04 18:56:40,593 : successful start loop run
INFO : 2018-11-04 18:56:45,729 : successful start loop run
INFO : 2018-11-04 18:57:27,543 : New program run

INFO : 2018-11-04 18:57:32,671 : successful start loop run
INFO : 2018-11-04 18:57:37,783 : successful start loop run
ERROR : 2018-11-04 18:57:37,916 :
Traceback (most recent call last):
File "C:\Users\fred laptop\Google Drive\Uni Work\Activity Led Learning Projects\Python_Telegram_Chat_bot\chat_bot_main.py", line 104, in chat_bot
logging.info('message_id = {}\ntext = {}\nreply = {]'.format(message_id,text,send))
ValueError: expected '}' before end of string
INFO : 2018-11-04 18:57:38,112 : message popped
INFO : 2018-11-04 18:57:43,113 : successful start loop run
INFO : 2018-11-04 18:57:48,538 : successful start loop run
INFO : 2018-11-04 18:58:24,070 : New program run

INFO : 2018-11-04 18:58:29,563 : successful start loop run
ERROR : 2018-11-04 18:58:29,693 :
Traceback (most recent call last):
File "C:\Users\fred laptop\Google Drive\Uni Work\Activity Led Learning Projects\Python_Telegram_Chat_bot\chat_bot_main.py", line 104, in chat_bot
logging.info('message_id = {}\ntext = {}\nreply = {]'.format(message_id,text,send))
ValueError: expected '}' before end of string
INFO : 2018-11-04 18:58:29,843 : message popped
INFO : 2018-11-04 18:59:00,148 : New program run

INFO : 2018-11-04 18:59:00,720 : message_id = 517
text = /start
reply = Hi there, I'm py_chat_bot! I was built by a small group of students at unviersity over a six week period. I can make use of the Google API, provide games to play, and make general conversation. We would love for you to provide any feedback to us to the contacts below :) Fred Cook - fredcook789@gmail.com Thankyou

INFO : 2018-11-04 18:59:00,936 : message popped
INFO : 2018-11-04 18:59:05,937 : successful start loop run
INFO : 2018-11-04 18:59:11,177 : successful start loop run
INFO : 2018-11-04 18:59:16,293 : successful start loop run
INFO : 2018-11-04 18:59:21,412 : successful start loop run
INFO : 2018-11-04 18:59:40,151 : New program run

INFO : 2018-11-04 18:59:45,275 : successful start loop run
ERROR : 2018-11-04 18:59:45,427 :
Traceback (most recent call last):
File "C:\Users\fred laptop\Google Drive\Uni Work\Activity Led Learning Projects\Python_Telegram_Chat_bot\chat_bot_main.py", line 103, in chat_bot
logging.info('message_id = {}\ntext = {}\nreply = {]'.format(message_id,text,send))
ValueError: expected '}' before end of string
INFO : 2018-11-04 18:59:45,614 : message popped
INFO : 2018-11-04 18:59:50,622 : successful start loop run
INFO : 2018-11-04 18:59:55,737 : successful start loop run
INFO : 2018-11-04 19:00:01,281 : successful start loop run
INFO : 2018-11-04 19:00:06,390 : successful start loop run
INFO : 2018-11-04 19:00:11,591 : successful start loop run
INFO : 2018-11-04 19:00:16,749 : successful start loop run
INFO : 2018-11-04 19:00:23,878 : successful start loop run
INFO : 2018-11-04 19:00:28,862 : New program run

INFO : 2018-11-04 19:00:29,029 : successful start loop run
ERROR : 2018-11-04 19:00:31,111 :
Traceback (most recent call last):
File "C:\Users\fred laptop\Google Drive\Uni Work\Activity Led Learning Projects\Python_Telegram_Chat_bot\chat_bot_main.py", line 103, in chat_bot
logging.info(('message_id = {}\ntext = {}\nreply = {]').format(message_id,text,send))
ValueError: expected '}' before end of string
INFO : 2018-11-04 19:00:31,239 : message popped
INFO : 2018-11-04 19:00:34,106 : successful start loop run
INFO : 2018-11-04 19:00:36,258 : successful start loop run
INFO : 2018-11-04 19:00:39,211 : successful start loop run
INFO : 2018-11-04 19:00:39,417 : New program run

INFO : 2018-11-04 19:00:49,039 : successful start loop run
INFO : 2018-11-04 19:00:49,366 : message popped
INFO : 2018-11-04 19:00:54,367 : successful start loop run
INFO : 2018-11-04 19:00:59,919 : successful start loop run
INFO : 2018-11-04 19:01:05,041 : successful start loop run
INFO : 2018-11-04 19:01:10,152 : successful start loop run
INFO : 2018-11-04 19:01:15,267 : successful start loop run
INFO : 2018-11-04 19:01:20,410 : successful start loop run
INFO : 2018-11-04 19:01:25,513 : successful start loop run
INFO : 2018-11-04 19:01:33,379 : successful start loop run
INFO : 2018-11-04 19:01:38,495 : successful start loop run
INFO : 2018-11-04 19:01:43,631 : successful start loop run
INFO : 2018-11-04 19:01:46,017 : New program run

INFO : 2018-11-04 19:01:48,676 : message_id = 525
text = G
reply = Thank you for the message Fred Cook

INFO : 2018-11-04 19:01:48,677 : message popped
INFO : 2018-11-04 19:01:48,751 : successful start loop run
INFO : 2018-11-04 19:01:49,432 : message popped
INFO : 2018-11-04 19:01:53,677 : successful start loop run
INFO : 2018-11-04 19:01:54,496 : successful start loop run
INFO : 2018-11-04 19:01:58,602 : message_id = 528
text = H
reply = Thank you for the message Fred Cook

INFO : 2018-11-04 19:01:58,602 : message popped
INFO : 2018-11-04 19:01:58,625 : message popped
INFO : 2018-11-04 19:02:03,646 : successful start loop run
INFO : 2018-11-04 19:02:03,647 : successful start loop run
INFO : 2018-11-04 19:02:08,809 : successful start loop run
INFO : 2018-11-04 19:02:11,790 : successful start loop run
INFO : 2018-11-04 19:02:14,863 : New program run

INFO : 2018-11-04 19:02:19,988 : successful start loop run
INFO : 2018-11-04 19:02:20,601 : message_id = 531
text = G
reply = Thank you for the message Fred Cook

INFO : 2018-11-04 19:02:20,601 : message popped
INFO : 2018-11-04 19:02:25,603 : successful start loop run
INFO : 2018-11-05 03:01:53,483 : New program run

INFO : 2018-11-05 03:02:03,136 : New program run

INFO : 2018-11-05 03:02:08,284 : successful start loop run
INFO : 2018-11-05 03:02:32,469 : New program run

INFO : 2018-11-05 03:02:37,598 : successful start loop run
@@ -1,29 +1,38 @@
#another restart of chat_bot
import json
import requests #must be installed through pip
import webbrowser
import time
# another restart of chat_bot
import requests #must be installed through pip
import json #standard library
import webbrowser #standard library
import time #standard library
import logging #standard library

logging.basicConfig(filename='chat_bot_logs.txt', level = logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s')

my_bot_token = "755874438:AAHYURHnfsUjWWrX_FxwA85P1yhzRVB_SJ0"
receive_messages_url = "https://api.telegram.org/bot755874438:AAHYURHnfsUjWWrX_FxwA85P1yhzRVB_SJ0/getUpdates"
acknowledge_messages = "https://api.telegram.org/bot755874438:AAHYURHnfsUjWWrX_FxwA85P1yhzRVB_SJ0/getUpdates?offset={}" # tells the receive_messages_url
# tells the receive_messages_url
acknowledge_messages = "https://api.telegram.org/bot755874438:AAHYURHnfsUjWWrX_FxwA85P1yhzRVB_SJ0/getUpdates?offset={}"
# that all messages up to message id given have been processed and can be discarded so we need to do this


#anything added onto the end of the "send_messages_url" is the message that is sent to the user
#chat_id = the user that the message is sent to
#chat_id must be formatted using "send_messages_url.format(chat_id,message)" (because of the square braces)
# anything added onto the end of the "send_messages_url" is the message that is sent to the user
# chat_id = the user that the message is sent to
# chat_id must be formatted using "send_messages_url.format(chat_id,message)" (because of the square braces)
send_messages_url = "https://api.telegram.org/bot755874438:AAHYURHnfsUjWWrX_FxwA85P1yhzRVB_SJ0/sendMessage?chat_id={}&text={}"

"""reads the logs from the "receive_messages_url" and gets the json information, converts it into a py dictionary"""
"""this function was taken from the tutorial at 'https://www.codementor.io/garethdwyer/building-a-telegram-bot-using-python-part-1-goi5fncay'"""


def get_url(url):
out = requests.get(url)
out = out.content.decode("utf8")
out = json.loads(out)
return out


"""checks for new messages from the get_url function and places them into an array for individual processing"""


def new_messages_array():
message_array = []
JSON = get_url(receive_messages_url)
@@ -33,32 +42,42 @@ def new_messages_array():


"""the below functions get get essential information about messages from the message given to them from the new_messages_array"""


def get_user_id(message):
out = message['message']['from']['id']
return out


def get_first_name(message):
out = message['message']['from']['first_name']
return out


def get_last_name(message):
out = message['message']['from']['last_name']
return out


def get_update_id(message):
out = message['update_id']
return out


def get_message_id(message):
out = message['message']['message_id']
return out


def get_text(message):
out = message['message']['text']
return out


"""this is the function that will generate the responses and give them to the user"""
def chat_bot(message): #always takes in only a single message from message_array


def chat_bot(message): # always takes in only a single message from message_array
try:
user_id = get_user_id(message)
first_name = get_first_name(message)
@@ -67,36 +86,56 @@ def chat_bot(message): #always takes in only a single message from message_array
message_id = get_message_id(message)
text = get_text(message)

if text == "/start": #first message that the bot will send to the user when the /start command is sent
send = "Hi there, I'm py_chat_bot! I was built by a small group of students at unviersity over a six week period. I can make use of the Google API, provide games to play, and make general conversation. We would love for you to provide any feedback to us to the contacts below :) Fred Cook - fredcook789@gmail.com Thankyou" #can put in more emails but need to check if its okay with others first
#manage to get this to output in multiple lines then format it to do so
webbrowser.open(send_messages_url.format(user_id,send))
webbrowser.open(acknowledge_messages.format(update_id+1))
if text == "/start": # first message that the bot will send to the user when the /start command is sent
# can put in more emails but need to check if its okay with others first
send = "Hi there, I'm py_chat_bot! I was built by a small group of students at unviersity over a six week period. I can make use of the Google API, provide games to play, and make general conversation. We would love for you to provide any feedback to us to the contacts below :) Fred Cook - fredcook789@gmail.com Thankyou"
# manage to get this to output in multiple lines then format it to do so
logging.info(('message_id = {} \n text = {}\n reply = {} \n').format(message_id, text, send)) #these can be removed, used for debugging and development
webbrowser.open(send_messages_url.format(user_id, send))
webbrowser.open(acknowledge_messages.format(update_id + 1))

else:
send = ("thank you for the message {} {}") #THIS NEEDS TO CHANGE, THIS IS WHAT IS SENT TO USER
# THIS NEEDS TO CHANGE, THIS IS WHAT IS SENT TO USER
send = ("thank you for the message {} {}")
send = send.capitalize()
send = send.format(first_name,last_name)
print("message {} replied to. text = {}. Reply = {}".format(message_id,text, send))
webbrowser.open(send_messages_url.format(user_id,send))
webbrowser.open(acknowledge_messages.format(update_id+1))
send = send.format(first_name, last_name)
print("message {} replied to. text = {}. Reply = {}".format(message_id, text, send))
webbrowser.open(send_messages_url.format(user_id, send))
webbrowser.open(acknowledge_messages.format(update_id + 1))
logging.info(('message_id = {} \n text = {}\n reply = {} \n').format(message_id, text, send))

except:
webbrowser.open(send_messages_url.format(user_id, "sorry, I don't understand that".capitalize()))
logging.exception('')
webbrowser.open(send_messages_url.format(
user_id, "sorry, I don't understand that".capitalize()))
webbrowser.open(acknowledge_messages.format(update_id + 1))
print("invalid message received")


"""this is the function that starts the program, it could be made into a separate 'main' file, it calls the other functions and at the end it 'acknowledges messages' as to allow for more than 100 messages to be on the receive_messages_url at any given time"""


def start():
a = True
logging.info('New program run \n')
while (a == True):
new_message_array = new_messages_array()
while len(new_message_array) > 0:
chat_bot(new_message_array[0])
new_message_array.pop(0)
logging.info('message popped')
print("No new messages")
time.sleep(5) #THIS CAN BE REMOVED, THIS IS SUPPOSED TO SIMULATE THE CHAT_BOT FUNCTION TAKING TIME
#need to be able toclear the receive_message_url and marks them all as replied to
# THIS CAN BE REMOVED, THIS IS SUPPOSED TO SIMULATE THE CHAT_BOT FUNCTION TAKING TIME
time.sleep(5) #may need to keep this as telegram api rules state a maximum polling rate, if we don't receive enough messages we will have to keep this, potentially increase the time

# need to be able toclear the receive_message_url and marks them all as replied to
logging.info('successful start loop run')

if __name__ == "__main__":
start()

print(start())
#TODO: change logging format
#TODO: make actual chatbot
#TODO: move different functions in to different files t omake it easier to collaborate on
#TODO: set up long polling ideally
#TODO: design a way to have the program decide if the input from the user is soemthing that the google APIs should be queried for e.g. "where is the nearest restaurant?", or if it is soemthing that should be sent to the chat bot function so maybe something like if 'nearest in message ... go to google APIs else... go to chat_bot

This file was deleted.

0 comments on commit a32ac2c

Please sign in to comment.