Twitter application – automated call center

This is a code for an application that connects to twitter API, fetch mentions, look for questions then send the answers back 🙂 don’t hesitate to ask any question 🙂
Please copy and paste it on ur IDLE python to c it nicer!

Don’t forget to register in twitter developer to get ur keys for ur application, plus installing the needed libraries
If u have no idea about what I’m saying please follow the link 🙂 below

http://m.youtube.com/watch?v=-3Vu2A4bzjQ

————————————————————

#call-center application, using Python 2.7
#it connects to twitter api through a twitter account,get most 20 recent mentions
#then looks for questions,if found;analyze it by tracing the decision tree
#then send reply with the answer to the user who asked the question -I’m printing users’s info just for tracing!-.
#remember, you can always make your artificial call center smarter by extending the tree to cover more questions!
#Impotent Note!!: before closing the application,you need to save/write the ID of last processed tweet into file or something to avoid sending same questions to ppl every time u run it!
#which means u need to read it first from the file at the beginning of the code,i skipped this,modify the code if u need!:)
#GoodLuck!
#by @eng_mariam86, Mariam alzuwawi

import simplejson # you need simplejson,httplib2,twitter libraries to connect to twitter api,use easy_install for python to install them better!
import httplib2
import re, twitter
import time
from datetime import datetime

api = twitter.Api(consumer_key=’I removed mine,put urs’, consumer_secret=’removed mine’, access_token_key=’removed mine’, access_token_secret=’removed mine’)

last_tweet = 0

search_tag = re.compile(‘when’) #set the value of the root

def PrintInfo(x): #function to print info whenever a search tag is found at the leaf nodes
print x.text
print ‘User Screen Name :’, x.user.screen_name
print ‘Description :’, x.user.description
print ‘Created at:’, x.user.created_at
print ‘Followers :’, x.user.followers_count
print ‘Following :’, x.user.friends_count
print ‘Time Zone :’, x.user.time_zone
print ‘User ID :’, x.id
print ‘geographic location :’, x.geo
print ‘Location :’, x.location
print ‘end of tweet\n\n\n’

try:

while True:# loop until CTRL+C is tapped or exit()

if(last_tweet == 0):#variable to save the last ID of processed tweet to avoid processeing it twice!

statuses = statuses = api.GetMentions() # get mentions of the twitter acount

else:

statuses = api.GetMentions(since_id = last_tweet)

for s in statuses:

search_result = search_tag.search(s.text) #look for the search_tag in a tweet text

#for large scale application, algorithms like j48 must be used to generate the decision tree for efficiency
#the code bellow is for decision tree done manually by not -not algorithm!- just for simplicity and simulation, besides, it cover only few questions!

if search_result: # ‘when’ is found

if re.search(re.compile(‘registration’), s.text, flags=0): #’registration’ is found, can extend the search to ‘register’

api.PostUpdate(‘@’+s.user.screen_name+’ Registration for the 1st semester starts on 13-5-2012 \n *generated at’+str(datetime.now()), in_reply_to_status_id = s.id)#reply to mention and can be used to post a status
PrintInfo(s) #date and time is generated to avoid posting duplicated statuses which is not allowed by twitter

elif re.search(re.compile(‘vacation’), s.text, flags=0):

api.PostUpdate(‘@’+s.user.screen_name+’ summer vacation starts on 1-6-2012 \n *generated at’+str(datetime.now()), in_reply_to_status_id = s.id)
PrintInfo(s)

elif re.search(re.compile(‘fees’), s.text, flags=0): #can extend the search to ‘fee’ , ‘payment and ‘pay’

api.PostUpdate(‘@’+s.user.screen_name+’ payments for the semester starts on 22-5-2012 to 24-5-2012 \n *generated at’+str(datetime.now()), in_reply_to_status_id = s.id)
PrintInfo(s)

#else: api.PostUpdate(‘@’+s.user.screen_name+’ Please define the Question \n *generated at’+str(datetime.now()), in_reply_to_status_id = s.id)

elif re.search(re.compile(‘what’), s.text, flags=0):#if ‘when’ is not found, look for ‘what’

if re.search(re.compile(‘fee’), s.text, flags=0):

api.PostUpdate(‘@’+s.user.screen_name+’ fees = 30 k.d. per course + 40 k.d. for registration \n *generated at’+str(datetime.now()), in_reply_to_status_id = s.id)
PrintInfo(s)

#else: api.PostUpdate(‘@’+s.user.screen_name+’ Please define the Question \n *generated at’+str(datetime.now()), in_reply_to_status_id = s.id)

elif re.search(re.compile(‘how’), s.text, flags=0): # if ‘what is not found, look for ‘how’

if re.search(re.compile(‘register’), s.text, flags=0): # if ‘register’ is found

api.PostUpdate(‘@’+s.user.screen_name+’ please visit kuwait university website for more info https://portal.ku.edu.kw/sisapp/faces/login.jspx \n *generated at’+str(datetime.now()), in_reply_to_status_id = s.id)
PrintInfo(s)

#else: api.PostUpdate(‘@’+s.user.screen_name+’ Please define the Question \n *generated at’+str(datetime.now()), in_reply_to_status_id = s.id) #can use this if needed

else: api.PostUpdate(‘@’+s.user.screen_name+’ Please define the Question \n *generated at’+str(datetime.now()), in_reply_to_status_id = s.id) #if nothing found in the tree, end of tree

print “***END STATUS BLOCK*** \n\n”
print ‘please send a question’

if(statuses != []): #make sure statuses array isn’t empty
last_tweet = statuses[0].id

time.sleep(30) #wait before fetching the next block of tweets -the new ones of course!

except KeyboardInterrupt: #ends the script on keyboard interrupt

print “closing!” #the end:)

Posted in IT, review | Tagged | Leave a comment

Girls tools

When girls go for engineering 😄

They must have
Lovely cute tools 😉

٢٠١٢٠٤٠٨-١٠٠٧٥٠.jpg

٢٠١٢٠٤٠٨-١٠٠٨١٢.jpg

Posted in fun, pot | Tagged | Leave a comment

Rice pudding – kuwait

امس جربت rice pudding في منطقة النزهة بجانب شتار باكس 😊 وكان لذيذ صراحه بس يشبببع 🙂
جربت نكهة الكوكيز آند كريم مع كراميل و رازبيري
وانشاءالله لنا تجارب اخرى😉
اتمنى لهم كل التوفيق 🙂

٢٠١٢٠٤٠٧-١٥١٢٥١.jpg

٢٠١٢٠٤٠٧-١٥١٣٠٢.jpg

٢٠١٢٠٤٠٧-١٥١٣١٢.jpg

٢٠١٢٠٤٠٧-١٥١٣٢٥.jpg

٢٠١٢٠٤٠٧-١٥١٥٥٥.jpg

٢٠١٢٠٤٠٧-١٥١٦٢٧.jpg

Posted in Food, review | Tagged | Leave a comment

صورة اليوم 😄

بصراحة الاعلان اتعبني نفسياً وارهقني بصرياً
اي امان انا بغيت ادعم وانا اصور من الصدمة😝
لا وعلى جسر المشكلة🙆 يارباه

٢٠١٢٠٤٠٥-١٥٣٦٣١.jpg

٢٠١٢٠٤٠٥-١٤٤٢٠٦.jpg

Posted in fun, pot | Tagged | 1 Comment

Chocolateness

٢٠١٢٠٢٢٣-١٩١٣٣٢.jpg

Chocolateness celebrating national day 🙂

For orders: 22960606

Image | Posted on by | Tagged | Leave a comment

My new place

But under construction 😉 at kuwait university IT
Of course i should have those pretty things with me from home 🙂

٢٠١٢٠٢٢١-٠٨٣٥٣٥.jpg

٢٠١٢٠٢٢١-٠٨٣٦٤٥.jpg

Image | Posted on by | Tagged | Leave a comment

Chocomania

٢٠١٢٠٢١٩-٢٣١٠٥٤.jpg

New shower cream from the body shop 🙂
Nice & delecioso 😉

Image | Posted on by | Leave a comment