Skip to content

Instantly share code, notes, and snippets.

@sae13
Last active June 14, 2017 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sae13/f9ad7adc12801ba833a2b22cb54c5673 to your computer and use it in GitHub Desktop.
Save sae13/f9ad7adc12801ba833a2b22cb54c5673 to your computer and use it in GitHub Desktop.
a telegram bot for making long url short
import urllib.parse, urllib.request
import telegram
import logging
from telegram.error import NetworkError, Unauthorized
from time import sleep
from khayyam import JalaliDatetime
import validators
import json
update_id = None
bot_started = JalaliDatetime.now()
def main():
global update_id
# Telegram Bot Authorization Token
bot = telegram.Bot('YOUR BOT TOKEN')
# get the first pending update_id, this is so we can skip over it in case
# we get an "Unauthorized" exception.
try:
update_id = bot.get_updates()[0].update_id
except IndexError:
update_id = None
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
while True:
try:
echo(bot)
except NetworkError:
sleep(1)
except Unauthorized:
# The user has removed or blocked the bot.
update_id += 1
def echo(bot):
global update_id
# Request updates after the last update_id
for update in bot.get_updates(offset=update_id, timeout=10):
update_id = update.update_id + 1
if update.message: # your bot can receive updates without messages
# Reply to the message
if update.message.text:
if update.message.text == "/start":
update.message.reply_text("باسلام و احترام.این بات لینک کوتاه میسازه"
"\n"
"عقیل دردا بلات بابت وی پی اس"
"\n"
"@WISxDOM"
"\n"
"سورس بات رو میتونید اینجا مشاهده کنید:"
"\n"
"http://sae13.ir/archives/587"
"\n"
"@saeb_m"
"\n"
"لینک طولانی تون رو وارد کنید: "
"\n"
)
break
if update.message.text.lower() == 'uptime' :
now_time = JalaliDatetime.now()
uptime = now_time - bot_started
days_uptime = uptime.seconds // (3600 * 24)
hours_uptime = (uptime.seconds % (3600 * 24)) // 3600
minutes_uptime =((uptime.seconds % (3600 * 24)) % 3600) //60
bot.send_message(update.message.chat.id,"مدت زمانی فعالیت بات:"
"\n "
"{} روز و"
"\n"
" {}ساعت "
"\n"
"{}دقیقه".format(days_uptime,
hours_uptime,
minutes_uptime))
break
if validators.url(update.message.text):
long_url = update.message.text
values = {"access_token": YOUR BITLY TOKEN",
"domain": "j.mp",
"longUrl":long_url}
endpoint = 'https://api-ssl.bitly.com/v3/shorten'
data = urllib.parse.urlencode(values)
data = data.encode('ascii')
req = urllib.request.Request(endpoint, data)
response = urllib.request.urlopen(req)
responsed = response.read().decode('utf8')
jresponsed = json.loads(responsed)
print(responsed)
if jresponsed['data']:
if jresponsed['data']['url']:
shorturl = jresponsed['data']['url']
update.message.reply_text("آدرس کوتاه به "
"\n"
"{}"
"\n"
"میشه:"
"\n"
"{}".format(long_url, shorturl))
break
else:
update.message.reply_text("به نظر میاد لینکی وارد کردید درست نیست")
# update.message.reply_text('{}\n{}\n{}\n'.format(first, secound, third))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment