SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 21/01/2019, 10:34 AM   #1
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 2,056
Reputation: 494
Default Telegram Connector

TgConnector



A telegram connector plugin that helps to interact with telgram bots through SA-MP.

Installing

If you are a sampctl user

Code:
sampctl p install Sreyas-Sreelal/tgconnector


OR
  • Download suitable binary files from releases for your operating system
  • Add it your plugins folder
  • Add tgconnector to server.cfg or tgconnector.so (for linux)
  • Add tgconnector.inc in includes folder

Building


Example

A basic bot

pawn Code:
#include<a_samp>
#include<tgconnector>
#include<zcmd>

#define CHAT_ID (TGChatId:"YOUR_CHAT_ID_HERE")

new TGBot:g_bot;

main() {
    //Store bot token in SAMP_TG_BOT environment variable and connect from it
    g_bot = TGConnectFromEnv("SAMP_TG_BOT");
    if(g_bot != INVALID_BOT_ID) {
        printf("bot connected successfully!");
    } else {
        printf("Error: bot couldn't connect");
    }
}

public OnTGMessage(TGBot:bot,TGUser:fromid,TGMessage:messageid) {
   
    if(g_bot != bot){
        return 1;
    }

    new
        message[50],
        username[24],
        chatname[56],
        server_msg[128];

    TGCacheGetMessage(message);
    TGCacheGetUserName(username);
    TGCacheGetChatName(chatname);
   
    format(server_msg,128,"[%s] %s(%d): %s",chatname,username,_:fromid,message);
    SendClientMessageToAll(-1,server_msg);
   
    return 1;
}


public OnTGUserJoined(TGBot:bot,TGUser:userid) {
    new
        TGChatId:chatid[15],
        username[24],
        chatname[56],
        server_msg[128];
   
    //Retrive data stored in current context
    TGCacheGetUserName(username);
    TGCacheGetChatId(chatid);
    TGCacheGetChatName(chatname);

    format(server_msg,128,"User %s(%d) joined %s(%s)",username,_:userid,chatname,_:chatid);
    SendClientMessageToAll(-1,server_msg);
    return 1;
}

public OnTGUserLeft(TGBot:bot,TGUser:userid) {
    new
        TGChatId:chatid[15],
        username[24],
        chatname[56],
        server_msg[128];
   
    TGCacheGetUserName(username);
    TGCacheGetChatId(chatid);
    TGCacheGetChatName(chatname);

    format(server_msg,128,"User %s(%d) left %s(%s)",username,_:userid,chatname,_:chatid);
    SendClientMessageToAll(-1,server_msg);
    return 1;
}

CMD:sendtgmessage(playerid,params[]) {
    TGSendMessage(g_bot,CHAT_ID,params);
    return 1;
}

Notes
This plugin is still in WIP and more tests need to be done.If you find any bugs or have anything to contribute feel free to open an issue or pull request on github.
Also be sure to not to share your bot token with anyone it's recommended to store it inside a environment variable.

Repository
Source: https://github.com/Sreyas-Sreelal/tgconnector
Releases: https://github.com/Sreyas-Sreelal/tgconnector/releases
Wiki: https://github.com/Sreyas-Sreelal/tgconnector/wiki (Not complete yet)

Last edited by SyS; 22/01/2019 at 03:44 PM.
SyS is offline   Reply With Quote
Old 21/01/2019, 11:21 AM   #2
Hazon
Big Clucker
 
Hazon's Avatar
 
Join Date: May 2018
Location: Croatia
Posts: 87
Reputation: 4
Default Re: Telegram Connector

Going to test, wd.
Hazon is offline   Reply With Quote
Old 22/01/2019, 07:08 AM   #3
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 2,056
Reputation: 494
Default Re: Telegram Connector

New version (v0.1.0)
https://github.com/Sreyas-Sreelal/tg...ases/tag/0.1.0
  • Channel updates are now serialized correctly.
  • New callback OnTGChannelPost has been added.
SyS is offline   Reply With Quote
Old 22/01/2019, 01:21 PM   #4
NimA00GaMeR
Little Clucker
 
NimA00GaMeR's Avatar
 
Join Date: Sep 2018
Location: Iran
Posts: 13
Reputation: 0
Default Re: Telegram Connector

Wowwww.
It's Great!
NimA00GaMeR is offline   Reply With Quote
Old 22/01/2019, 02:23 PM   #5
Kar
High-roller
 
Kar's Avatar
 
Join Date: May 2010
Location: Black Asylum
Posts: 3,171
Reputation: 511
Default Re: Telegram Connector

Lol telegram, good job!
Kar is offline   Reply With Quote
Old 25/01/2019, 11:00 PM   #6
Chaprnks
Gangsta
 
Chaprnks's Avatar
 
Join Date: Sep 2007
Location: Soviet America
Posts: 762
Reputation: 64
Default Re: Telegram Connector

Thank you so much for this plugin!


Update:
I can't for the life of me, seem to figure out how to find the chat id of my channel.. even tried using the GetIDsBot telegram bot with no luck.. lol
__________________

Last edited by Chaprnks; 26/01/2019 at 02:11 AM.
Chaprnks is offline   Reply With Quote
Old 26/01/2019, 03:18 AM   #7
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 2,056
Reputation: 494
Default Re: Telegram Connector

Quote:
Originally Posted by Chaprnks View Post
Thank you so much for this plugin!


Update:
I can't for the life of me, seem to figure out how to find the chat id of my channel.. even tried using the GetIDsBot telegram bot with no luck.. lol
There are couple of tricks to get chat id of a private channel.Here's one you can do in pawn itself.

Add your bot as admin in your channel.
Make channel public and set a user name (username is one along with invite link).
Use TGSendMessage with chatid as user username with a prefix '@' and specify a callback.
Use TGCacheGetChatId to get chatid stored inside the cache.

For example my channel' username is samptest
Use following snippet

PHP Code:
new TGBot:g_bot;

main() {
    
g_bot TGConnectFromEnv("SAMP_TG_BOT");
    
TGSendMessage(g_bot,TGChatId:"@samptest","test message",.callback="OnChannelSendMessage");
}

forward OnChannelSendMessage(TGBot:bot,TGMessage:messageid);
public 
OnChannelSendMessage(TGBot:bot,TGMessage:messageid) {
    new 
TGChatId:chatid[15];
    
TGCacheGetChatId(chatid);
    
printf("the chatid is %s",_:chatid);
    return 
1;

Now you can set the channel back to private.
You can also do the same by sending an api request in your browser with chatid parameter as your channel username or use OnTGChannelPost callback and send a message to your channel manually and use TGCacheGetChatId there.
SyS is offline   Reply With Quote
Old 26/01/2019, 07:30 AM   #8
Chaprnks
Gangsta
 
Chaprnks's Avatar
 
Join Date: Sep 2007
Location: Soviet America
Posts: 762
Reputation: 64
Default Re: Telegram Connector

Quote:
Originally Posted by SyS View Post
There are couple of tricks to get chat id of a private channel.Here's one you can do in pawn itself.

Add your bot as admin in your channel.
Make channel public and set a user name (username is one along with invite link).
Use TGSendMessage with chatid as user username with a prefix '@' and specify a callback.
Use TGCacheGetChatId to get chatid stored inside the cache.

For example my channel' username is samptest
Use following snippet

PHP Code:
new TGBot:g_bot;

main() {
    
g_bot TGConnectFromEnv("SAMP_TG_BOT");
    
TGSendMessage(g_bot,TGChatId:"@samptest","test message",.callback="OnChannelSendMessage");
}

forward OnChannelSendMessage(TGBot:bot,TGMessage:messageid);
public 
OnChannelSendMessage(TGBot:bot,TGMessage:messageid) {
    new 
TGChatId:chatid[15];
    
TGCacheGetChatId(chatid);
    
printf("the chatid is %s",_:chatid);
    return 
1;

Now you can set the channel back to private.
You can also do the same by sending an api request in your browser with chatid parameter as your channel username or use OnTGChannelPost callback and send a message to your channel manually and use TGCacheGetChatId there.
Thank you so much! It's odd how they make it so difficult to find something as simple as a ID field..
__________________
Chaprnks is offline   Reply With Quote
Old 26/01/2019, 08:17 AM   #9
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 2,056
Reputation: 494
Default Re: Telegram Connector

Quote:
Originally Posted by Chaprnks View Post
Thank you so much! It's odd how they make it so difficult to find something as simple as a ID field..
It's true.But still forwarding message to @get_id_bot is easier way and idk why it didn't work for you.
SyS is offline   Reply With Quote
Old 07/02/2019, 02:25 AM   #10
GhostHacker9
Big Clucker
 
GhostHacker9's Avatar
 
Join Date: Jan 2017
Location: New Zealand
Posts: 107
Reputation: 109
Default Re: Telegram Connector

Wtf why I didn't see this before.This is so great!Thank you so much
GhostHacker9 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Run Discord Connector ?? RJTabish Scripting Help 4 28/09/2018 12:40 PM
TeamSpeak Connector Suttix Scripting Help 5 10/12/2017 11:23 AM
route connector CodeStyle175 Scripting Help 1 11/11/2017 08:55 PM
[Map] Connector Gas Tellken Maps 4 01/06/2012 04:04 AM
PSU connector help! patchkinson Everything and Nothing 3 20/03/2010 10:24 PM


All times are GMT. The time now is 01:15 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.