makaleler / Python Programlama / python clickatell üzerinden SMS mesajı göndermek

python clickatell üzerinden SMS mesajı göndermek

09.01.2011 12:52:38

Bunu Emrah abi yazmıştı teşekkürler Emrah abi.

Python ile yazılmış bu betik, Clickatell üzerinden SMS gönderir. SMS gönderebilmek için öncelikle Clickatell hesabı açtırmak ve HTTPS API almak gerekmektedir.

#!/usr/bin/python
# -*- coding:utf-8 -*-

import re
import sys
import urllib

FROM                = 'gonderen'                                                                # mesaji gonderen kisi
CALLBACK            = 0                                                                         # geri cevap gonderilecek mi
                                                                                                #   0 -> gonderilmeyecek
                                                                                                #   1 -> anlik durum gonderilecek
                                                                                                #   2 -> son durum gonderilecek
                                                                                                #   3 -> hem anlik hem de son durum gonderilecek
API_ID              = '0000000'                                                                 # Api numarasi
KULLANICI           = 'kullanici'                                                               # kullanici adi
PAROLA              = 'parola'                                                                  # kullanici parolasi
SABLON_SESSION_ID   = re.compile('^OK: (.+?)$')                                                 # session_id parse eden sablon
SABLON_BALANCE      = re.compile('^Credit: ([0-9\.]+?)$')                                       # balance parse eden sablon
SABLON_SEND         = re.compile('^ID: (.+?)$')                                                 # send_id parse eden sablon
DEBUG               = 1


# ---------------------------------------------------
# session id alan bolum
def getSessionId():
    try:
        params = urllib.urlencode({'api_id':API_ID, 'user':KULLANICI, 'password':PAROLA})
        cnn = urllib.urlopen('[url]https://api.clickatell.com/http/auth?%s[/url]' % params)
        str = cnn.read()
        if DEBUG: print('SESSION ID', str)

        g = SABLON_SESSION_ID.search(str)
        if g    : return (True, g.group(1))
        else    : raise RuntimeError
    except:
        if DEBUG: print('SESSION ERROR')
        return (False, '')


# ---------------------------------------------------
# balance alan bolum
def getBalance(session_id):
    try:
        params = urllib.urlencode({'session_id':session_id})
        cnn = urllib.urlopen('[url]https://api.clickatell.com/http/getbalance?%s[/url]' % params)
        str = cnn.read()
        if DEBUG: print('BALANCE', str)

        g = SABLON_BALANCE.search(str)
        if g    : return float(g.group(1))
        else    : raise RuntimeError
    except:
        if DEBUG: print('BALANCE ERROR')
        return 0.0


# ---------------------------------------------------
# mesaj gonderen bolum
def send(session_id, balance, to, from_, mesaj, callback):
    try:
        # balance yoksa mesaj gonderemez
        if balance < 1: raise RuntimeError
        # to temizle
        to = to_temizle(to)
        # to veya from_ yoksa hata var demektir
        if not to or not from_: raise RuntimeError
        # mesaj boyu 160 karakterden uzun olamaz
        if len(mesaj) > 160: raise RuntimeError

        # mesaji gonder
        params = urllib.urlencode({'session_id':session_id, 'to':to, 'from':from_, 'text':mesaj, 'callback':callback})
        cnn = urllib.urlopen("[url]https://api.clickatell.com/http/sendmsg?%s[/url]" % params)
        str = cnn.read()
        if DEBUG: print('SEND', str)

        g = SABLON_SEND.search(str)
        if g    : return (True, g.group(1))
        else    : raise RuntimeError
    except:
        if DEBUG: print('SEND ERROR')
        return (False, '')


# ---------------------------------------------------
# to kismindaki rakkam olmayan karakterleri temizleyen bolum
def to_temizle(to):
    try:
        for c in to:
            if not c.isdigit(): to = to.replace(c,'')
        return to
    except:
        return ''


if __name__ == '__main__':
    (session_kuruldu_mu, session_id) = getSessionId()
    if not session_kuruldu_mu: sys.exit(1)
    balance = getBalance(session_id)
    if not balance: sys.exit(1)

    numara = '90555XXXXXXX'
    mesaj  = 'bu test mesaji gonderilecek'
    (gonderildi_mi, send_id) = send(session_id, balance, numara, FROM, mesaj, CALLBACK)


İlgili Konular
Türktelekom üzerinden sms göndermek /makale.py?tid=16
yazar husonet

Yorumlar

Bu içerik için sizde yorum yapabilirsiniz!
anasayfa | makaleler | haberler | dosyalar | linkler | hakkımızda